diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..416f609 --- /dev/null +++ b/main.c @@ -0,0 +1,424 @@ +/******************************************************************************* + * @file main.c + * @author casic 203 + * @version V1.0 + * @date 2019-06-14 + * @brief + * @attention +*********************************************************************************/ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "AiderProtocol.h" +#include "API.h" +#include "sleep.h" +#include "math.h" +#include "NB-IOT.h" +#include "flash.h" + +#include "gsdk_sdk.h" +#include +#include +#include "gsdk_api.h" + +struct DeviceSet DeviceConfig = {0x00}; //������Ϣ�ṹ�� +struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +struct BusinessData businessData; +struct DataFrame dataFrame; + +uint8_t DeviceID[] = DEVICE_ID; //��ʼ���豸ID�� +uint16_t NodeAddr =0x0000; +uint8_t RouteControl =0x03; //·�ɿ��Ʊ�����Ĭ��ʹ�����ݼ������������ݴ��� +uint8_t send_buff[300]= {'\0'}; +uint8_t history_send_buff[300]= {'\0'}; +double GASData=200; +uint16_t ACR; + +static gsdk_handle_t g_huart; +char TestReceiveBuff[300]; + +gsdk_handle_t hgpio; + +double Gas_DataGet(void); +extern uint8_t SIMCard_Type; + +uint8_t PD2_Flag = 0;//��ñ��־��1����203��ƽ̨������ + +int __io_puts(char *data, int len) +{ + int ret = 0; + if (g_huart) + { + ret = gsdk_uart_write(g_huart, (uint8_t *)data, len, 1000); + } + return ret; +} + +int log_init(void) +{ + uart_config_t uart_config; + gsdk_status_t status; + + uart_config.baudrate = UART_BAUDRATE_115200; + uart_config.parity = UART_PARITY_NONE; + uart_config.stop_bit = UART_STOP_BIT_1; + uart_config.word_length = UART_WORD_LENGTH_8; + + status = gsdk_uart_open(UART_0, &uart_config, &g_huart); + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[GPIO_DEMO]: failed to open uart %d\n", status); + return -1; + } + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t Gpio_Init(void) +{ + gsdk_status_t status; + gpio_config_t gpio_config; + gpio_pin_t gpio_pin; + + gpio_pin = GPIO_PIN_9; + + gpio_config.direction = GPIO_DIRECTION_OUTPUT; + gpio_config.pull = GPIO_PULL_NONE; + gpio_config.int_mode = GPIO_INT_DISABLE; + gpio_config.debounce_time = 0; + gpio_config.callback = NULL; + + status = gsdk_gpio_open(gpio_pin, &gpio_config, &hgpio); + if (status != GSDK_SUCCESS) + { + goto _fail; + } + + return 0; + +_fail: + gsdk_gpio_close(hgpio); + printf("[GPIO_DEMO] Gpio test failed!\n"); + return -1; +} + +void gpio_callback(void *user_data) +{ + printf("[GPIO_DEMO] gpio eint handle callback!\n"); +} + +/******************************************************************************* +* Function Name : void PeripheralInit(void) +* Description : ��ʼ���˿ڼ����� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t PeripheralInit(void) +{ + uint8_t Boot_Mode_Flag = 0; + uint8_t count = 0; + uint8_t RunCount = 0; + + Gpio_Init(); + log_init(); + gsdk_gpio_write(hgpio, GPIO_DATA_HIGH);//��ȼ��̽ͷ��Դ + Boot_Mode_Flag = Get_Boot_Mode(); + bsp_Init_DS2782(); + gsdk_ril_init(); + + Module_Flash_Read(RUN_COUNT_ADDR, &RunCount, 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (RunCount > 24) //�豸�����ⲿ��λ + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + else if (RunCount == 24) + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + RunCount++; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + printf("\n\r RunCount:%d \r\n", RunCount); + + gsdk_ril_set_ip_mode(IPV4); + gsdk_ril_set_cfun_mode(1); // ����ģ��ĵ绰����Ϊ1 - ȫ���� + while (1) + { + if (count > 6) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(300); + RTC_Init(); //��ȡϵͳ��ǰʱ�� + + printf("\n\r****************************************************************\r\n"); + printf("\n\r*-----��ӭʹ��BIRMM-GT200N ȼ�����ܼ��ܶ�!---------------*\r\n"); + printf("\n\r*-----����汾�ţ�%s ------------------------*\r\n", SOFTWARE_VERSION); + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + printf("\n\r*-----��Ȩ���У��������ߵ���������о���------------------*\r\n"); + printf("\n\r****************************************************************\r\n"); + + return Boot_Mode_Flag; +} + +/******************************************************************************* +* Function Name : int main(void) +* Description : ������ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int main(void) +{ + uint16_t Boot_Mode_Flag, StartupResponse_Flag; + + Boot_Mode_Flag = PeripheralInit(); //��ʼ������ + ConfigData_Init(); //��FLASH��ȡ���ò��� + +/* Module_DS2781_Set_Register(); + vTaskDelay(100); + + while(1) + { + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + printf("\r\n Get_DS2782_RAAC() is : %d \r\n", Get_DS2782_RAAC()); + printf("\r\n Get_DS2782_RSAC() is : %d \r\n", Get_DS2782_RSAC()); + printf("\r\n Get_DS2782_RSRC() is : %d \r\n", Get_DS2782_RSRC()); + + vTaskDelay(100); + }*/ + + //�����⵽���磬�ϴ����� + if(Boot_Mode_Flag == 2) + { +// RTC_Timing(1); + PD2_Flag = 1; + Module_DS2781_Set_Register(); + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + Code_Read_NB(); + StartupResponse_Flag = StartupRequest(send_buff,8);//������֡�ϴ� + if(StartupResponse_Flag==0x099C) printf("\r\n IMEI ICCID �ϱ��ɹ�����\r\n"); + else printf("\r\n IMEI ICCID �ϱ�ʧ�ܣ���\r\n"); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else if(Boot_Mode_Flag == 3) + { + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� + } + + if(businessData.SendCount>0) //������ʷ���� + { + Code_Read_NB(); + + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + SendMessage_NB_T(history_send_buff, businessData.HistoryDateCount); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + SendMessage_NB_MU(history_send_buff, businessData.HistoryDateCount); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + if(businessData.ParameterReply>0) //������ʷ���� + { + Code_Read_NB(); + StartupRequest(send_buff,12);//�������ûظ��ϴ� + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + + DeviceConfig.CollectPeriod = 1; + + /*����Ѳɼ����ݴ��ڲɼ�����*/ + if((businessData.DataCount >= DeviceConfig.CollectNum)) + { + Code_Read_NB(); + StartupResponse_Flag = TrapRequest(send_buff,4);//������֡�ϴ� + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + BusinessData_Collection(); + if(GASData <=100) + { + BusinessData_Storage(); + } + else{ + Code_Read_NB(); + TrapRequest(send_buff,4);//������֡�ϴ� + } + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� +} + +double Gas_DataGet(void) +{ + uint16_t TestReceiveLength = 0; + char *pRecevBuff = NULL; + char *pGasRecevBuff = NULL; + char RecevFromGas[4] = {0x20,0x30,0x30,0x20}; + char RecevFromRadar[2] = {0x0d,0x0a}; + uint8_t j = 0; + uint8_t pp,i,m; + char h1; + char s1[2]; + GASData=250; + uint8_t count = 0; + + printf("\r\n Ԥ��8��... \r\n"); + while (1) + { + if (count > 32) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(800);//Ԥ��8�� + + printf("\r\n Ԥ�Ƚ���... \r\n"); + for(j = 0; j < 30; j++) + { + printf("\r\n ��ʼ����... \r\n"); + TestReceiveLength = gsdk_uart_read(g_huart, (uint8_t *)TestReceiveBuff, 300, 1000); + printf("\r\n TestReceiveLength is : %d \r\n", TestReceiveLength); + if(TestReceiveLength > 0) + { + pGasRecevBuff = Find_SpecialString(TestReceiveBuff+180, RecevFromGas, strlen(TestReceiveBuff), 4); + if(pGasRecevBuff != NULL) //��ָֹ��Խ�� + { + printf("\r\n pGasRecevBuff is : %s \r\n", pGasRecevBuff); + pRecevBuff = Find_SpecialString(pGasRecevBuff, RecevFromRadar, strlen(pGasRecevBuff), 2); + for(m = 0; m < 29; m++) + { + printf("\r\npRecevBuff---------- %.2x\r\n", pRecevBuff[m]); + } + if(pRecevBuff != NULL) //��ָֹ��Խ�� + { + if((pRecevBuff[9]==0x20)&&(pRecevBuff[15]==0x20)&&(pRecevBuff[23]==0x20)&&(pRecevBuff[26]==0x20)) + { + //printf("\r\nUART4�������ݽ���!!\r\n"); + pp= pRecevBuff[2]; + for(i=1; i<25; i++) + { + pp^= pRecevBuff[i+2]; + } + s1[0]= pRecevBuff[27]; + s1[1]= pRecevBuff[28]; + string_to_hex(&h1,s1); + + printf("\r\nUART4 У�� %.2x %.2x!!\r\n",pp,h1); + if(pp== h1) //��λ���У�� + { + if(pRecevBuff[24]==0x30) //�����ź� + { + printf("\r\npRecevBuff[25]---------------------------- %.2x!!\r\n",pRecevBuff[25]); + if(pRecevBuff[25]==0x30) //�����ź� + { + printf("\r\n ����״̬���� \r\n"); + if( pRecevBuff[6]==0x2e ) //С���� + { + GASData =(pRecevBuff[3]-0x30)*100 + (pRecevBuff[4]-0x30)*10 + (pRecevBuff[5]-0x30)*1.0 + (pRecevBuff[7]-0x30)*0.1 + (pRecevBuff[8]-0x30)*0.01 ; + if(pRecevBuff[2]==0x2b ) // ������ + { + + } + else if(pRecevBuff[2]==0x2d) // ������ + { + GASData=-GASData; + } + else + { + GASData =250;//ȼ��������ͨ���쳣 + } + } + } + else if(pRecevBuff[25]==0x31) + { + printf("\r\n ��·�쳣���޷�����ʹ�� \r\n"); + GASData =240; + } + else if(pRecevBuff[25]==0x32) // + { + printf("\r\n �¶�ѹ���쳣 \r\n"); + GASData =240; + } + } + else + { + printf("\r\n ȼ��̽ͷ�쳣 \r\n"); + } + } + } + } + } + } + if(GASData <=100) break; + while (1) + { + if (count > 2) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(100); + memset(TestReceiveBuff, 0x00, 300); + } + printf("\r\n GASData is : %d \r\n", (uint8_t)GASData); + + gsdk_gpio_write(hgpio, GPIO_DATA_LOW);//�ر�ȼ��̽ͷ��Դ + return GASData; +} \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..416f609 --- /dev/null +++ b/main.c @@ -0,0 +1,424 @@ +/******************************************************************************* + * @file main.c + * @author casic 203 + * @version V1.0 + * @date 2019-06-14 + * @brief + * @attention +*********************************************************************************/ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "AiderProtocol.h" +#include "API.h" +#include "sleep.h" +#include "math.h" +#include "NB-IOT.h" +#include "flash.h" + +#include "gsdk_sdk.h" +#include +#include +#include "gsdk_api.h" + +struct DeviceSet DeviceConfig = {0x00}; //������Ϣ�ṹ�� +struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +struct BusinessData businessData; +struct DataFrame dataFrame; + +uint8_t DeviceID[] = DEVICE_ID; //��ʼ���豸ID�� +uint16_t NodeAddr =0x0000; +uint8_t RouteControl =0x03; //·�ɿ��Ʊ�����Ĭ��ʹ�����ݼ������������ݴ��� +uint8_t send_buff[300]= {'\0'}; +uint8_t history_send_buff[300]= {'\0'}; +double GASData=200; +uint16_t ACR; + +static gsdk_handle_t g_huart; +char TestReceiveBuff[300]; + +gsdk_handle_t hgpio; + +double Gas_DataGet(void); +extern uint8_t SIMCard_Type; + +uint8_t PD2_Flag = 0;//��ñ��־��1����203��ƽ̨������ + +int __io_puts(char *data, int len) +{ + int ret = 0; + if (g_huart) + { + ret = gsdk_uart_write(g_huart, (uint8_t *)data, len, 1000); + } + return ret; +} + +int log_init(void) +{ + uart_config_t uart_config; + gsdk_status_t status; + + uart_config.baudrate = UART_BAUDRATE_115200; + uart_config.parity = UART_PARITY_NONE; + uart_config.stop_bit = UART_STOP_BIT_1; + uart_config.word_length = UART_WORD_LENGTH_8; + + status = gsdk_uart_open(UART_0, &uart_config, &g_huart); + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[GPIO_DEMO]: failed to open uart %d\n", status); + return -1; + } + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t Gpio_Init(void) +{ + gsdk_status_t status; + gpio_config_t gpio_config; + gpio_pin_t gpio_pin; + + gpio_pin = GPIO_PIN_9; + + gpio_config.direction = GPIO_DIRECTION_OUTPUT; + gpio_config.pull = GPIO_PULL_NONE; + gpio_config.int_mode = GPIO_INT_DISABLE; + gpio_config.debounce_time = 0; + gpio_config.callback = NULL; + + status = gsdk_gpio_open(gpio_pin, &gpio_config, &hgpio); + if (status != GSDK_SUCCESS) + { + goto _fail; + } + + return 0; + +_fail: + gsdk_gpio_close(hgpio); + printf("[GPIO_DEMO] Gpio test failed!\n"); + return -1; +} + +void gpio_callback(void *user_data) +{ + printf("[GPIO_DEMO] gpio eint handle callback!\n"); +} + +/******************************************************************************* +* Function Name : void PeripheralInit(void) +* Description : ��ʼ���˿ڼ����� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t PeripheralInit(void) +{ + uint8_t Boot_Mode_Flag = 0; + uint8_t count = 0; + uint8_t RunCount = 0; + + Gpio_Init(); + log_init(); + gsdk_gpio_write(hgpio, GPIO_DATA_HIGH);//��ȼ��̽ͷ��Դ + Boot_Mode_Flag = Get_Boot_Mode(); + bsp_Init_DS2782(); + gsdk_ril_init(); + + Module_Flash_Read(RUN_COUNT_ADDR, &RunCount, 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (RunCount > 24) //�豸�����ⲿ��λ + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + else if (RunCount == 24) + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + RunCount++; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + printf("\n\r RunCount:%d \r\n", RunCount); + + gsdk_ril_set_ip_mode(IPV4); + gsdk_ril_set_cfun_mode(1); // ����ģ��ĵ绰����Ϊ1 - ȫ���� + while (1) + { + if (count > 6) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(300); + RTC_Init(); //��ȡϵͳ��ǰʱ�� + + printf("\n\r****************************************************************\r\n"); + printf("\n\r*-----��ӭʹ��BIRMM-GT200N ȼ�����ܼ��ܶ�!---------------*\r\n"); + printf("\n\r*-----����汾�ţ�%s ------------------------*\r\n", SOFTWARE_VERSION); + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + printf("\n\r*-----��Ȩ���У��������ߵ���������о���------------------*\r\n"); + printf("\n\r****************************************************************\r\n"); + + return Boot_Mode_Flag; +} + +/******************************************************************************* +* Function Name : int main(void) +* Description : ������ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int main(void) +{ + uint16_t Boot_Mode_Flag, StartupResponse_Flag; + + Boot_Mode_Flag = PeripheralInit(); //��ʼ������ + ConfigData_Init(); //��FLASH��ȡ���ò��� + +/* Module_DS2781_Set_Register(); + vTaskDelay(100); + + while(1) + { + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + printf("\r\n Get_DS2782_RAAC() is : %d \r\n", Get_DS2782_RAAC()); + printf("\r\n Get_DS2782_RSAC() is : %d \r\n", Get_DS2782_RSAC()); + printf("\r\n Get_DS2782_RSRC() is : %d \r\n", Get_DS2782_RSRC()); + + vTaskDelay(100); + }*/ + + //�����⵽���磬�ϴ����� + if(Boot_Mode_Flag == 2) + { +// RTC_Timing(1); + PD2_Flag = 1; + Module_DS2781_Set_Register(); + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + Code_Read_NB(); + StartupResponse_Flag = StartupRequest(send_buff,8);//������֡�ϴ� + if(StartupResponse_Flag==0x099C) printf("\r\n IMEI ICCID �ϱ��ɹ�����\r\n"); + else printf("\r\n IMEI ICCID �ϱ�ʧ�ܣ���\r\n"); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else if(Boot_Mode_Flag == 3) + { + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� + } + + if(businessData.SendCount>0) //������ʷ���� + { + Code_Read_NB(); + + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + SendMessage_NB_T(history_send_buff, businessData.HistoryDateCount); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + SendMessage_NB_MU(history_send_buff, businessData.HistoryDateCount); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + if(businessData.ParameterReply>0) //������ʷ���� + { + Code_Read_NB(); + StartupRequest(send_buff,12);//�������ûظ��ϴ� + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + + DeviceConfig.CollectPeriod = 1; + + /*����Ѳɼ����ݴ��ڲɼ�����*/ + if((businessData.DataCount >= DeviceConfig.CollectNum)) + { + Code_Read_NB(); + StartupResponse_Flag = TrapRequest(send_buff,4);//������֡�ϴ� + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + BusinessData_Collection(); + if(GASData <=100) + { + BusinessData_Storage(); + } + else{ + Code_Read_NB(); + TrapRequest(send_buff,4);//������֡�ϴ� + } + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� +} + +double Gas_DataGet(void) +{ + uint16_t TestReceiveLength = 0; + char *pRecevBuff = NULL; + char *pGasRecevBuff = NULL; + char RecevFromGas[4] = {0x20,0x30,0x30,0x20}; + char RecevFromRadar[2] = {0x0d,0x0a}; + uint8_t j = 0; + uint8_t pp,i,m; + char h1; + char s1[2]; + GASData=250; + uint8_t count = 0; + + printf("\r\n Ԥ��8��... \r\n"); + while (1) + { + if (count > 32) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(800);//Ԥ��8�� + + printf("\r\n Ԥ�Ƚ���... \r\n"); + for(j = 0; j < 30; j++) + { + printf("\r\n ��ʼ����... \r\n"); + TestReceiveLength = gsdk_uart_read(g_huart, (uint8_t *)TestReceiveBuff, 300, 1000); + printf("\r\n TestReceiveLength is : %d \r\n", TestReceiveLength); + if(TestReceiveLength > 0) + { + pGasRecevBuff = Find_SpecialString(TestReceiveBuff+180, RecevFromGas, strlen(TestReceiveBuff), 4); + if(pGasRecevBuff != NULL) //��ָֹ��Խ�� + { + printf("\r\n pGasRecevBuff is : %s \r\n", pGasRecevBuff); + pRecevBuff = Find_SpecialString(pGasRecevBuff, RecevFromRadar, strlen(pGasRecevBuff), 2); + for(m = 0; m < 29; m++) + { + printf("\r\npRecevBuff---------- %.2x\r\n", pRecevBuff[m]); + } + if(pRecevBuff != NULL) //��ָֹ��Խ�� + { + if((pRecevBuff[9]==0x20)&&(pRecevBuff[15]==0x20)&&(pRecevBuff[23]==0x20)&&(pRecevBuff[26]==0x20)) + { + //printf("\r\nUART4�������ݽ���!!\r\n"); + pp= pRecevBuff[2]; + for(i=1; i<25; i++) + { + pp^= pRecevBuff[i+2]; + } + s1[0]= pRecevBuff[27]; + s1[1]= pRecevBuff[28]; + string_to_hex(&h1,s1); + + printf("\r\nUART4 У�� %.2x %.2x!!\r\n",pp,h1); + if(pp== h1) //��λ���У�� + { + if(pRecevBuff[24]==0x30) //�����ź� + { + printf("\r\npRecevBuff[25]---------------------------- %.2x!!\r\n",pRecevBuff[25]); + if(pRecevBuff[25]==0x30) //�����ź� + { + printf("\r\n ����״̬���� \r\n"); + if( pRecevBuff[6]==0x2e ) //С���� + { + GASData =(pRecevBuff[3]-0x30)*100 + (pRecevBuff[4]-0x30)*10 + (pRecevBuff[5]-0x30)*1.0 + (pRecevBuff[7]-0x30)*0.1 + (pRecevBuff[8]-0x30)*0.01 ; + if(pRecevBuff[2]==0x2b ) // ������ + { + + } + else if(pRecevBuff[2]==0x2d) // ������ + { + GASData=-GASData; + } + else + { + GASData =250;//ȼ��������ͨ���쳣 + } + } + } + else if(pRecevBuff[25]==0x31) + { + printf("\r\n ��·�쳣���޷�����ʹ�� \r\n"); + GASData =240; + } + else if(pRecevBuff[25]==0x32) // + { + printf("\r\n �¶�ѹ���쳣 \r\n"); + GASData =240; + } + } + else + { + printf("\r\n ȼ��̽ͷ�쳣 \r\n"); + } + } + } + } + } + } + if(GASData <=100) break; + while (1) + { + if (count > 2) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(100); + memset(TestReceiveBuff, 0x00, 300); + } + printf("\r\n GASData is : %d \r\n", (uint8_t)GASData); + + gsdk_gpio_write(hgpio, GPIO_DATA_LOW);//�ر�ȼ��̽ͷ��Դ + return GASData; +} \ No newline at end of file diff --git a/rtc.c b/rtc.c new file mode 100644 index 0000000..450f4c7 --- /dev/null +++ b/rtc.c @@ -0,0 +1,340 @@ + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "rtc.h" +#include "AiderProtocol.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:F:%s,L:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +extern struct DeviceSet DeviceConfig; + +static gsdk_handle_t rtc_handle; + +void rtc_alarm_cb(void *user_data) +{ + (void)user_data; + gsdk_ril_init(); + gsdk_ril_set_hardware_reboot();//����ģ�� + OEM_LOGI("[RTC_DEMO] rtc alarm handle cb------------------------0!\n"); +} + +void utc_to_bj_time(rtc_time_t rtc_time) +{ + int year,month,day,hour; + int lastday = 0; //last day of this month + int lastlastday = 0; //last day of last month + char temp_buf[100] = {0}; + + year = rtc_time.rtc_year + 2000; //utc time + month = rtc_time.rtc_mon; + day = rtc_time.rtc_day; + hour = rtc_time.rtc_hour + 8; + + if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) + { + lastday = 31; + lastlastday = 30;//����Ӧ������ϸ��µ����� + + if(month == 3) + { + if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year + lastlastday = 29; + else + lastlastday = 28; + } + + if(month == 8 || month == 1)//����Ӧ����8�º�1�£���Ϊ8�º�1�µ���һ���µ�������31��� + lastlastday = 31; + } + else if(month == 4 || month == 6 || month == 9 || month == 11) + { + lastday = 30; + lastlastday = 31; + } + else + { + lastlastday = 31; + + if((year%400 == 0)||(year%4 == 0 && year%100 != 0)) + lastday = 29; + else + lastday = 28; + } + + if(hour >= 24) // if >24, day+1 + { + hour -= 24; + day += 1; + + if(day > lastday) // next month, day-lastday of this month + { + day -= lastday; + month += 1; + + if(month > 12) // next year , month-12 + { + month -= 12; + year += 1; + } + } + } + if(hour < 0) // if <0, day-1 + { + hour += 24; + day -= 1; + if(day < 1) // month-1, day=last day of last month + { + day = lastlastday; + month -= 1; + if(month < 1) // last year , month=12 + { + month = 12; + year -= 1; + } + } + } + + sprintf(temp_buf, + "Beijing time,%4d,%02d,%02d,%02d,%02d,%02d", + year, + month, + day, + hour, + rtc_time.rtc_min, + rtc_time.rtc_sec); + + OEM_LOGI("��ǰʱ��: %s!\n", temp_buf); + + DeviceConfig.Time_Year = year-2000;/* ϵͳ���ڣ�OID��0x10000050 */ + DeviceConfig.Time_Mon = month; + DeviceConfig.Time_Mday = day; + DeviceConfig.Time_Hour = hour; + DeviceConfig.Time_Min = rtc_time.rtc_min; +} + +/******************************************************************************* +* Function Name : RTC_Init +* Description : RTC��ʼ������ȡϵͳʱ�� +* Input : struct DeviceSet* DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Init(void) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + status = gsdk_rtc_open(&rtc_handle); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_open failed!\n"); + goto _fail; + } + + status = gsdk_rtc_time_read(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_time_read failed!\n"); + goto _fail; + } + + utc_to_bj_time(rtc_time); + + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Init failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : RTC_Timing +* Description : RTCУʱ +* Input : uint8_t SetTime_Flag��1ʱ��ָ��������ã�2ʱ������Ϊ������Уʱʱ�䣩, struct DeviceSet DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Timing(uint8_t SetTime_Flag) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + if(SetTime_Flag == 1) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = 21; + rtc_time.rtc_mon = 1; + rtc_time.rtc_day = 1; + rtc_time.rtc_week = 1; + rtc_time.rtc_hour = 1; + rtc_time.rtc_min = 1; + rtc_time.rtc_sec = 1; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + else if(SetTime_Flag == 2) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = DeviceConfig.Time_Year; + rtc_time.rtc_mon = DeviceConfig.Time_Mon; + rtc_time.rtc_day = DeviceConfig.Time_Mday; + rtc_time.rtc_hour = DeviceConfig.Time_Hour; + rtc_time.rtc_min = DeviceConfig.Time_Min; + rtc_time.rtc_sec = DeviceConfig.Time_Sec; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Timing failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : Set_Alarm +* Description : RTC�������� +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : 1�ɹ���0ʧ�� +*******************************************************************************/ +void Set_Alarm(long SleepPara) +{ + gsdk_status_t status; + int i,j; + gsdk_handle_t g_alarm_handle; + uint8_t count = 0; + + if(SleepPara > 0) + { + /*configure alarm timer property*/ + gsdk_alarm_config_t alarm_config; + alarm_config.callback = rtc_alarm_cb;//timer callback + alarm_config.interval = SleepPara;//��λ��s + alarm_config.periodic = 1;//timer perform periodically + status = gsdk_alarm_open(&g_alarm_handle, &alarm_config);//open alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("open alarm failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("open alarm success\r\n"); + status = gsdk_alarm_start(g_alarm_handle);//start up alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("gsdk_alarm_start failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("start alarm success\r\n"); + } + gsdk_rtc_close(rtc_handle); + + for(i=0; i<3; i++) + { + status = gsdk_ril_psm_set(CPSMS, "AT+CPSMS=1");//set "CPSMS=1" is power saving mode(PSM) + if(GSDK_ERROR == status) + { + DEBUG_LOG("set CPSMS=1 cmd error\r\n"); + goto _fail; + } + WARN_LOG("set CPSM=1 cmd success\r\n"); + + status = gsdk_ril_psm_set(ZSLR, "AT+ZSLR");//sleep command + if(GSDK_ERROR == status) + { + DEBUG_LOG("set ZSLR cmd error\r\n"); + goto _fail; + } + + WARN_LOG("wait for sleep....\r\n"); + for(j=0; j<3; j++) + { + DEBUG_LOG("-->ME3616 IS SLEEP <--\r\n"); + while (1) + { + if (count > 20) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(1000); //��ʱ10S���ȴ�˯�� + DEBUG_LOG("-->ME3616 IS WAKE <--\r\n"); + } + } + + gsdk_ril_set_hardware_reboot(); + +_fail: + WARN_LOG("Set_Alarm fail....\r\n"); + gsdk_ril_reset_module(); +} + +/******************************************************************************* +* Function Name : Get_Boot_Mode +* Description : ��ȡNBģ������ģʽ +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : Boot_Mode_Flag +*******************************************************************************/ +uint8_t Get_Boot_Mode(void) +{ + gsdk_boot_mode_t boot_mode; + gsdk_rtc_wakeup_mode_t wakeup_mode; + uint8_t Boot_Mode_Flag = 0; + + /*get boot mode function ,because this demo maybe enter PSM */ + boot_mode = gsdk_sys_get_boot_mode(&wakeup_mode); + DEBUG_LOG("boot_mode is---------------------------------------------- :%d\r\n", boot_mode); + if (1 == boot_mode || 2 == boot_mode) + { +// printf("wakeup from deep sleep\r\n"); + switch (wakeup_mode) + { + case GSDK_BOOT_RTC_TC_WAKEUP: + WARN_LOG("it's awakened by RTC_TC\r\n"); + break; + case GSDK_BOOT_RTC_EINT_WAKEUP: + WARN_LOG("it's awakened by RTC_EINT\r\n"); + break; + case GSDK_BOOT_RTC_ALARM_WAKEUP: + WARN_LOG("it's awakened by ALARM_WAKEUP\r\n"); + Boot_Mode_Flag = 1; + break; + case GSDK_BOOT_POWERKEY_WAKEUP: + WARN_LOG("it's awakened by POWERKEY\r\n"); + break; + default: + WARN_LOG("unknown mode\r\n"); + break; + } + } + else if (5 == boot_mode || 6 == boot_mode) + { + Boot_Mode_Flag = 3; + WARN_LOG("GSDK_BOOT_WDT_SW_RESET\r\n"); + } + else + { + Boot_Mode_Flag = 2; + WARN_LOG("not enter deep sleep\r\n"); + } + return Boot_Mode_Flag; +} \ No newline at end of file diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..416f609 --- /dev/null +++ b/main.c @@ -0,0 +1,424 @@ +/******************************************************************************* + * @file main.c + * @author casic 203 + * @version V1.0 + * @date 2019-06-14 + * @brief + * @attention +*********************************************************************************/ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "AiderProtocol.h" +#include "API.h" +#include "sleep.h" +#include "math.h" +#include "NB-IOT.h" +#include "flash.h" + +#include "gsdk_sdk.h" +#include +#include +#include "gsdk_api.h" + +struct DeviceSet DeviceConfig = {0x00}; //������Ϣ�ṹ�� +struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +struct BusinessData businessData; +struct DataFrame dataFrame; + +uint8_t DeviceID[] = DEVICE_ID; //��ʼ���豸ID�� +uint16_t NodeAddr =0x0000; +uint8_t RouteControl =0x03; //·�ɿ��Ʊ�����Ĭ��ʹ�����ݼ������������ݴ��� +uint8_t send_buff[300]= {'\0'}; +uint8_t history_send_buff[300]= {'\0'}; +double GASData=200; +uint16_t ACR; + +static gsdk_handle_t g_huart; +char TestReceiveBuff[300]; + +gsdk_handle_t hgpio; + +double Gas_DataGet(void); +extern uint8_t SIMCard_Type; + +uint8_t PD2_Flag = 0;//��ñ��־��1����203��ƽ̨������ + +int __io_puts(char *data, int len) +{ + int ret = 0; + if (g_huart) + { + ret = gsdk_uart_write(g_huart, (uint8_t *)data, len, 1000); + } + return ret; +} + +int log_init(void) +{ + uart_config_t uart_config; + gsdk_status_t status; + + uart_config.baudrate = UART_BAUDRATE_115200; + uart_config.parity = UART_PARITY_NONE; + uart_config.stop_bit = UART_STOP_BIT_1; + uart_config.word_length = UART_WORD_LENGTH_8; + + status = gsdk_uart_open(UART_0, &uart_config, &g_huart); + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[GPIO_DEMO]: failed to open uart %d\n", status); + return -1; + } + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t Gpio_Init(void) +{ + gsdk_status_t status; + gpio_config_t gpio_config; + gpio_pin_t gpio_pin; + + gpio_pin = GPIO_PIN_9; + + gpio_config.direction = GPIO_DIRECTION_OUTPUT; + gpio_config.pull = GPIO_PULL_NONE; + gpio_config.int_mode = GPIO_INT_DISABLE; + gpio_config.debounce_time = 0; + gpio_config.callback = NULL; + + status = gsdk_gpio_open(gpio_pin, &gpio_config, &hgpio); + if (status != GSDK_SUCCESS) + { + goto _fail; + } + + return 0; + +_fail: + gsdk_gpio_close(hgpio); + printf("[GPIO_DEMO] Gpio test failed!\n"); + return -1; +} + +void gpio_callback(void *user_data) +{ + printf("[GPIO_DEMO] gpio eint handle callback!\n"); +} + +/******************************************************************************* +* Function Name : void PeripheralInit(void) +* Description : ��ʼ���˿ڼ����� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t PeripheralInit(void) +{ + uint8_t Boot_Mode_Flag = 0; + uint8_t count = 0; + uint8_t RunCount = 0; + + Gpio_Init(); + log_init(); + gsdk_gpio_write(hgpio, GPIO_DATA_HIGH);//��ȼ��̽ͷ��Դ + Boot_Mode_Flag = Get_Boot_Mode(); + bsp_Init_DS2782(); + gsdk_ril_init(); + + Module_Flash_Read(RUN_COUNT_ADDR, &RunCount, 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (RunCount > 24) //�豸�����ⲿ��λ + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + else if (RunCount == 24) + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + RunCount++; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + printf("\n\r RunCount:%d \r\n", RunCount); + + gsdk_ril_set_ip_mode(IPV4); + gsdk_ril_set_cfun_mode(1); // ����ģ��ĵ绰����Ϊ1 - ȫ���� + while (1) + { + if (count > 6) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(300); + RTC_Init(); //��ȡϵͳ��ǰʱ�� + + printf("\n\r****************************************************************\r\n"); + printf("\n\r*-----��ӭʹ��BIRMM-GT200N ȼ�����ܼ��ܶ�!---------------*\r\n"); + printf("\n\r*-----����汾�ţ�%s ------------------------*\r\n", SOFTWARE_VERSION); + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + printf("\n\r*-----��Ȩ���У��������ߵ���������о���------------------*\r\n"); + printf("\n\r****************************************************************\r\n"); + + return Boot_Mode_Flag; +} + +/******************************************************************************* +* Function Name : int main(void) +* Description : ������ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int main(void) +{ + uint16_t Boot_Mode_Flag, StartupResponse_Flag; + + Boot_Mode_Flag = PeripheralInit(); //��ʼ������ + ConfigData_Init(); //��FLASH��ȡ���ò��� + +/* Module_DS2781_Set_Register(); + vTaskDelay(100); + + while(1) + { + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + printf("\r\n Get_DS2782_RAAC() is : %d \r\n", Get_DS2782_RAAC()); + printf("\r\n Get_DS2782_RSAC() is : %d \r\n", Get_DS2782_RSAC()); + printf("\r\n Get_DS2782_RSRC() is : %d \r\n", Get_DS2782_RSRC()); + + vTaskDelay(100); + }*/ + + //�����⵽���磬�ϴ����� + if(Boot_Mode_Flag == 2) + { +// RTC_Timing(1); + PD2_Flag = 1; + Module_DS2781_Set_Register(); + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + Code_Read_NB(); + StartupResponse_Flag = StartupRequest(send_buff,8);//������֡�ϴ� + if(StartupResponse_Flag==0x099C) printf("\r\n IMEI ICCID �ϱ��ɹ�����\r\n"); + else printf("\r\n IMEI ICCID �ϱ�ʧ�ܣ���\r\n"); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else if(Boot_Mode_Flag == 3) + { + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� + } + + if(businessData.SendCount>0) //������ʷ���� + { + Code_Read_NB(); + + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + SendMessage_NB_T(history_send_buff, businessData.HistoryDateCount); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + SendMessage_NB_MU(history_send_buff, businessData.HistoryDateCount); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + if(businessData.ParameterReply>0) //������ʷ���� + { + Code_Read_NB(); + StartupRequest(send_buff,12);//�������ûظ��ϴ� + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + + DeviceConfig.CollectPeriod = 1; + + /*����Ѳɼ����ݴ��ڲɼ�����*/ + if((businessData.DataCount >= DeviceConfig.CollectNum)) + { + Code_Read_NB(); + StartupResponse_Flag = TrapRequest(send_buff,4);//������֡�ϴ� + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + BusinessData_Collection(); + if(GASData <=100) + { + BusinessData_Storage(); + } + else{ + Code_Read_NB(); + TrapRequest(send_buff,4);//������֡�ϴ� + } + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� +} + +double Gas_DataGet(void) +{ + uint16_t TestReceiveLength = 0; + char *pRecevBuff = NULL; + char *pGasRecevBuff = NULL; + char RecevFromGas[4] = {0x20,0x30,0x30,0x20}; + char RecevFromRadar[2] = {0x0d,0x0a}; + uint8_t j = 0; + uint8_t pp,i,m; + char h1; + char s1[2]; + GASData=250; + uint8_t count = 0; + + printf("\r\n Ԥ��8��... \r\n"); + while (1) + { + if (count > 32) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(800);//Ԥ��8�� + + printf("\r\n Ԥ�Ƚ���... \r\n"); + for(j = 0; j < 30; j++) + { + printf("\r\n ��ʼ����... \r\n"); + TestReceiveLength = gsdk_uart_read(g_huart, (uint8_t *)TestReceiveBuff, 300, 1000); + printf("\r\n TestReceiveLength is : %d \r\n", TestReceiveLength); + if(TestReceiveLength > 0) + { + pGasRecevBuff = Find_SpecialString(TestReceiveBuff+180, RecevFromGas, strlen(TestReceiveBuff), 4); + if(pGasRecevBuff != NULL) //��ָֹ��Խ�� + { + printf("\r\n pGasRecevBuff is : %s \r\n", pGasRecevBuff); + pRecevBuff = Find_SpecialString(pGasRecevBuff, RecevFromRadar, strlen(pGasRecevBuff), 2); + for(m = 0; m < 29; m++) + { + printf("\r\npRecevBuff---------- %.2x\r\n", pRecevBuff[m]); + } + if(pRecevBuff != NULL) //��ָֹ��Խ�� + { + if((pRecevBuff[9]==0x20)&&(pRecevBuff[15]==0x20)&&(pRecevBuff[23]==0x20)&&(pRecevBuff[26]==0x20)) + { + //printf("\r\nUART4�������ݽ���!!\r\n"); + pp= pRecevBuff[2]; + for(i=1; i<25; i++) + { + pp^= pRecevBuff[i+2]; + } + s1[0]= pRecevBuff[27]; + s1[1]= pRecevBuff[28]; + string_to_hex(&h1,s1); + + printf("\r\nUART4 У�� %.2x %.2x!!\r\n",pp,h1); + if(pp== h1) //��λ���У�� + { + if(pRecevBuff[24]==0x30) //�����ź� + { + printf("\r\npRecevBuff[25]---------------------------- %.2x!!\r\n",pRecevBuff[25]); + if(pRecevBuff[25]==0x30) //�����ź� + { + printf("\r\n ����״̬���� \r\n"); + if( pRecevBuff[6]==0x2e ) //С���� + { + GASData =(pRecevBuff[3]-0x30)*100 + (pRecevBuff[4]-0x30)*10 + (pRecevBuff[5]-0x30)*1.0 + (pRecevBuff[7]-0x30)*0.1 + (pRecevBuff[8]-0x30)*0.01 ; + if(pRecevBuff[2]==0x2b ) // ������ + { + + } + else if(pRecevBuff[2]==0x2d) // ������ + { + GASData=-GASData; + } + else + { + GASData =250;//ȼ��������ͨ���쳣 + } + } + } + else if(pRecevBuff[25]==0x31) + { + printf("\r\n ��·�쳣���޷�����ʹ�� \r\n"); + GASData =240; + } + else if(pRecevBuff[25]==0x32) // + { + printf("\r\n �¶�ѹ���쳣 \r\n"); + GASData =240; + } + } + else + { + printf("\r\n ȼ��̽ͷ�쳣 \r\n"); + } + } + } + } + } + } + if(GASData <=100) break; + while (1) + { + if (count > 2) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(100); + memset(TestReceiveBuff, 0x00, 300); + } + printf("\r\n GASData is : %d \r\n", (uint8_t)GASData); + + gsdk_gpio_write(hgpio, GPIO_DATA_LOW);//�ر�ȼ��̽ͷ��Դ + return GASData; +} \ No newline at end of file diff --git a/rtc.c b/rtc.c new file mode 100644 index 0000000..450f4c7 --- /dev/null +++ b/rtc.c @@ -0,0 +1,340 @@ + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "rtc.h" +#include "AiderProtocol.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:F:%s,L:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +extern struct DeviceSet DeviceConfig; + +static gsdk_handle_t rtc_handle; + +void rtc_alarm_cb(void *user_data) +{ + (void)user_data; + gsdk_ril_init(); + gsdk_ril_set_hardware_reboot();//����ģ�� + OEM_LOGI("[RTC_DEMO] rtc alarm handle cb------------------------0!\n"); +} + +void utc_to_bj_time(rtc_time_t rtc_time) +{ + int year,month,day,hour; + int lastday = 0; //last day of this month + int lastlastday = 0; //last day of last month + char temp_buf[100] = {0}; + + year = rtc_time.rtc_year + 2000; //utc time + month = rtc_time.rtc_mon; + day = rtc_time.rtc_day; + hour = rtc_time.rtc_hour + 8; + + if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) + { + lastday = 31; + lastlastday = 30;//����Ӧ������ϸ��µ����� + + if(month == 3) + { + if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year + lastlastday = 29; + else + lastlastday = 28; + } + + if(month == 8 || month == 1)//����Ӧ����8�º�1�£���Ϊ8�º�1�µ���һ���µ�������31��� + lastlastday = 31; + } + else if(month == 4 || month == 6 || month == 9 || month == 11) + { + lastday = 30; + lastlastday = 31; + } + else + { + lastlastday = 31; + + if((year%400 == 0)||(year%4 == 0 && year%100 != 0)) + lastday = 29; + else + lastday = 28; + } + + if(hour >= 24) // if >24, day+1 + { + hour -= 24; + day += 1; + + if(day > lastday) // next month, day-lastday of this month + { + day -= lastday; + month += 1; + + if(month > 12) // next year , month-12 + { + month -= 12; + year += 1; + } + } + } + if(hour < 0) // if <0, day-1 + { + hour += 24; + day -= 1; + if(day < 1) // month-1, day=last day of last month + { + day = lastlastday; + month -= 1; + if(month < 1) // last year , month=12 + { + month = 12; + year -= 1; + } + } + } + + sprintf(temp_buf, + "Beijing time,%4d,%02d,%02d,%02d,%02d,%02d", + year, + month, + day, + hour, + rtc_time.rtc_min, + rtc_time.rtc_sec); + + OEM_LOGI("��ǰʱ��: %s!\n", temp_buf); + + DeviceConfig.Time_Year = year-2000;/* ϵͳ���ڣ�OID��0x10000050 */ + DeviceConfig.Time_Mon = month; + DeviceConfig.Time_Mday = day; + DeviceConfig.Time_Hour = hour; + DeviceConfig.Time_Min = rtc_time.rtc_min; +} + +/******************************************************************************* +* Function Name : RTC_Init +* Description : RTC��ʼ������ȡϵͳʱ�� +* Input : struct DeviceSet* DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Init(void) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + status = gsdk_rtc_open(&rtc_handle); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_open failed!\n"); + goto _fail; + } + + status = gsdk_rtc_time_read(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_time_read failed!\n"); + goto _fail; + } + + utc_to_bj_time(rtc_time); + + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Init failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : RTC_Timing +* Description : RTCУʱ +* Input : uint8_t SetTime_Flag��1ʱ��ָ��������ã�2ʱ������Ϊ������Уʱʱ�䣩, struct DeviceSet DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Timing(uint8_t SetTime_Flag) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + if(SetTime_Flag == 1) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = 21; + rtc_time.rtc_mon = 1; + rtc_time.rtc_day = 1; + rtc_time.rtc_week = 1; + rtc_time.rtc_hour = 1; + rtc_time.rtc_min = 1; + rtc_time.rtc_sec = 1; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + else if(SetTime_Flag == 2) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = DeviceConfig.Time_Year; + rtc_time.rtc_mon = DeviceConfig.Time_Mon; + rtc_time.rtc_day = DeviceConfig.Time_Mday; + rtc_time.rtc_hour = DeviceConfig.Time_Hour; + rtc_time.rtc_min = DeviceConfig.Time_Min; + rtc_time.rtc_sec = DeviceConfig.Time_Sec; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Timing failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : Set_Alarm +* Description : RTC�������� +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : 1�ɹ���0ʧ�� +*******************************************************************************/ +void Set_Alarm(long SleepPara) +{ + gsdk_status_t status; + int i,j; + gsdk_handle_t g_alarm_handle; + uint8_t count = 0; + + if(SleepPara > 0) + { + /*configure alarm timer property*/ + gsdk_alarm_config_t alarm_config; + alarm_config.callback = rtc_alarm_cb;//timer callback + alarm_config.interval = SleepPara;//��λ��s + alarm_config.periodic = 1;//timer perform periodically + status = gsdk_alarm_open(&g_alarm_handle, &alarm_config);//open alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("open alarm failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("open alarm success\r\n"); + status = gsdk_alarm_start(g_alarm_handle);//start up alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("gsdk_alarm_start failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("start alarm success\r\n"); + } + gsdk_rtc_close(rtc_handle); + + for(i=0; i<3; i++) + { + status = gsdk_ril_psm_set(CPSMS, "AT+CPSMS=1");//set "CPSMS=1" is power saving mode(PSM) + if(GSDK_ERROR == status) + { + DEBUG_LOG("set CPSMS=1 cmd error\r\n"); + goto _fail; + } + WARN_LOG("set CPSM=1 cmd success\r\n"); + + status = gsdk_ril_psm_set(ZSLR, "AT+ZSLR");//sleep command + if(GSDK_ERROR == status) + { + DEBUG_LOG("set ZSLR cmd error\r\n"); + goto _fail; + } + + WARN_LOG("wait for sleep....\r\n"); + for(j=0; j<3; j++) + { + DEBUG_LOG("-->ME3616 IS SLEEP <--\r\n"); + while (1) + { + if (count > 20) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(1000); //��ʱ10S���ȴ�˯�� + DEBUG_LOG("-->ME3616 IS WAKE <--\r\n"); + } + } + + gsdk_ril_set_hardware_reboot(); + +_fail: + WARN_LOG("Set_Alarm fail....\r\n"); + gsdk_ril_reset_module(); +} + +/******************************************************************************* +* Function Name : Get_Boot_Mode +* Description : ��ȡNBģ������ģʽ +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : Boot_Mode_Flag +*******************************************************************************/ +uint8_t Get_Boot_Mode(void) +{ + gsdk_boot_mode_t boot_mode; + gsdk_rtc_wakeup_mode_t wakeup_mode; + uint8_t Boot_Mode_Flag = 0; + + /*get boot mode function ,because this demo maybe enter PSM */ + boot_mode = gsdk_sys_get_boot_mode(&wakeup_mode); + DEBUG_LOG("boot_mode is---------------------------------------------- :%d\r\n", boot_mode); + if (1 == boot_mode || 2 == boot_mode) + { +// printf("wakeup from deep sleep\r\n"); + switch (wakeup_mode) + { + case GSDK_BOOT_RTC_TC_WAKEUP: + WARN_LOG("it's awakened by RTC_TC\r\n"); + break; + case GSDK_BOOT_RTC_EINT_WAKEUP: + WARN_LOG("it's awakened by RTC_EINT\r\n"); + break; + case GSDK_BOOT_RTC_ALARM_WAKEUP: + WARN_LOG("it's awakened by ALARM_WAKEUP\r\n"); + Boot_Mode_Flag = 1; + break; + case GSDK_BOOT_POWERKEY_WAKEUP: + WARN_LOG("it's awakened by POWERKEY\r\n"); + break; + default: + WARN_LOG("unknown mode\r\n"); + break; + } + } + else if (5 == boot_mode || 6 == boot_mode) + { + Boot_Mode_Flag = 3; + WARN_LOG("GSDK_BOOT_WDT_SW_RESET\r\n"); + } + else + { + Boot_Mode_Flag = 2; + WARN_LOG("not enter deep sleep\r\n"); + } + return Boot_Mode_Flag; +} \ No newline at end of file diff --git a/rtc.h b/rtc.h new file mode 100644 index 0000000..63f5cdb --- /dev/null +++ b/rtc.h @@ -0,0 +1,18 @@ +#ifndef __RTC_H +#define __RTC_H + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" + +int RTC_Init(void); //��ʼ��RTC +int RTC_Timing(uint8_t SetTime_Flag); +void Set_Alarm(long SleepPara); +uint8_t Get_Boot_Mode(void); +void utc_to_bj_time(rtc_time_t rtc_time); + +#endif + + diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..416f609 --- /dev/null +++ b/main.c @@ -0,0 +1,424 @@ +/******************************************************************************* + * @file main.c + * @author casic 203 + * @version V1.0 + * @date 2019-06-14 + * @brief + * @attention +*********************************************************************************/ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "AiderProtocol.h" +#include "API.h" +#include "sleep.h" +#include "math.h" +#include "NB-IOT.h" +#include "flash.h" + +#include "gsdk_sdk.h" +#include +#include +#include "gsdk_api.h" + +struct DeviceSet DeviceConfig = {0x00}; //������Ϣ�ṹ�� +struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +struct BusinessData businessData; +struct DataFrame dataFrame; + +uint8_t DeviceID[] = DEVICE_ID; //��ʼ���豸ID�� +uint16_t NodeAddr =0x0000; +uint8_t RouteControl =0x03; //·�ɿ��Ʊ�����Ĭ��ʹ�����ݼ������������ݴ��� +uint8_t send_buff[300]= {'\0'}; +uint8_t history_send_buff[300]= {'\0'}; +double GASData=200; +uint16_t ACR; + +static gsdk_handle_t g_huart; +char TestReceiveBuff[300]; + +gsdk_handle_t hgpio; + +double Gas_DataGet(void); +extern uint8_t SIMCard_Type; + +uint8_t PD2_Flag = 0;//��ñ��־��1����203��ƽ̨������ + +int __io_puts(char *data, int len) +{ + int ret = 0; + if (g_huart) + { + ret = gsdk_uart_write(g_huart, (uint8_t *)data, len, 1000); + } + return ret; +} + +int log_init(void) +{ + uart_config_t uart_config; + gsdk_status_t status; + + uart_config.baudrate = UART_BAUDRATE_115200; + uart_config.parity = UART_PARITY_NONE; + uart_config.stop_bit = UART_STOP_BIT_1; + uart_config.word_length = UART_WORD_LENGTH_8; + + status = gsdk_uart_open(UART_0, &uart_config, &g_huart); + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[GPIO_DEMO]: failed to open uart %d\n", status); + return -1; + } + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t Gpio_Init(void) +{ + gsdk_status_t status; + gpio_config_t gpio_config; + gpio_pin_t gpio_pin; + + gpio_pin = GPIO_PIN_9; + + gpio_config.direction = GPIO_DIRECTION_OUTPUT; + gpio_config.pull = GPIO_PULL_NONE; + gpio_config.int_mode = GPIO_INT_DISABLE; + gpio_config.debounce_time = 0; + gpio_config.callback = NULL; + + status = gsdk_gpio_open(gpio_pin, &gpio_config, &hgpio); + if (status != GSDK_SUCCESS) + { + goto _fail; + } + + return 0; + +_fail: + gsdk_gpio_close(hgpio); + printf("[GPIO_DEMO] Gpio test failed!\n"); + return -1; +} + +void gpio_callback(void *user_data) +{ + printf("[GPIO_DEMO] gpio eint handle callback!\n"); +} + +/******************************************************************************* +* Function Name : void PeripheralInit(void) +* Description : ��ʼ���˿ڼ����� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t PeripheralInit(void) +{ + uint8_t Boot_Mode_Flag = 0; + uint8_t count = 0; + uint8_t RunCount = 0; + + Gpio_Init(); + log_init(); + gsdk_gpio_write(hgpio, GPIO_DATA_HIGH);//��ȼ��̽ͷ��Դ + Boot_Mode_Flag = Get_Boot_Mode(); + bsp_Init_DS2782(); + gsdk_ril_init(); + + Module_Flash_Read(RUN_COUNT_ADDR, &RunCount, 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (RunCount > 24) //�豸�����ⲿ��λ + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + else if (RunCount == 24) + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + RunCount++; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + printf("\n\r RunCount:%d \r\n", RunCount); + + gsdk_ril_set_ip_mode(IPV4); + gsdk_ril_set_cfun_mode(1); // ����ģ��ĵ绰����Ϊ1 - ȫ���� + while (1) + { + if (count > 6) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(300); + RTC_Init(); //��ȡϵͳ��ǰʱ�� + + printf("\n\r****************************************************************\r\n"); + printf("\n\r*-----��ӭʹ��BIRMM-GT200N ȼ�����ܼ��ܶ�!---------------*\r\n"); + printf("\n\r*-----����汾�ţ�%s ------------------------*\r\n", SOFTWARE_VERSION); + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + printf("\n\r*-----��Ȩ���У��������ߵ���������о���------------------*\r\n"); + printf("\n\r****************************************************************\r\n"); + + return Boot_Mode_Flag; +} + +/******************************************************************************* +* Function Name : int main(void) +* Description : ������ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int main(void) +{ + uint16_t Boot_Mode_Flag, StartupResponse_Flag; + + Boot_Mode_Flag = PeripheralInit(); //��ʼ������ + ConfigData_Init(); //��FLASH��ȡ���ò��� + +/* Module_DS2781_Set_Register(); + vTaskDelay(100); + + while(1) + { + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + printf("\r\n Get_DS2782_RAAC() is : %d \r\n", Get_DS2782_RAAC()); + printf("\r\n Get_DS2782_RSAC() is : %d \r\n", Get_DS2782_RSAC()); + printf("\r\n Get_DS2782_RSRC() is : %d \r\n", Get_DS2782_RSRC()); + + vTaskDelay(100); + }*/ + + //�����⵽���磬�ϴ����� + if(Boot_Mode_Flag == 2) + { +// RTC_Timing(1); + PD2_Flag = 1; + Module_DS2781_Set_Register(); + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + Code_Read_NB(); + StartupResponse_Flag = StartupRequest(send_buff,8);//������֡�ϴ� + if(StartupResponse_Flag==0x099C) printf("\r\n IMEI ICCID �ϱ��ɹ�����\r\n"); + else printf("\r\n IMEI ICCID �ϱ�ʧ�ܣ���\r\n"); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else if(Boot_Mode_Flag == 3) + { + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� + } + + if(businessData.SendCount>0) //������ʷ���� + { + Code_Read_NB(); + + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + SendMessage_NB_T(history_send_buff, businessData.HistoryDateCount); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + SendMessage_NB_MU(history_send_buff, businessData.HistoryDateCount); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + if(businessData.ParameterReply>0) //������ʷ���� + { + Code_Read_NB(); + StartupRequest(send_buff,12);//�������ûظ��ϴ� + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + + DeviceConfig.CollectPeriod = 1; + + /*����Ѳɼ����ݴ��ڲɼ�����*/ + if((businessData.DataCount >= DeviceConfig.CollectNum)) + { + Code_Read_NB(); + StartupResponse_Flag = TrapRequest(send_buff,4);//������֡�ϴ� + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + BusinessData_Collection(); + if(GASData <=100) + { + BusinessData_Storage(); + } + else{ + Code_Read_NB(); + TrapRequest(send_buff,4);//������֡�ϴ� + } + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� +} + +double Gas_DataGet(void) +{ + uint16_t TestReceiveLength = 0; + char *pRecevBuff = NULL; + char *pGasRecevBuff = NULL; + char RecevFromGas[4] = {0x20,0x30,0x30,0x20}; + char RecevFromRadar[2] = {0x0d,0x0a}; + uint8_t j = 0; + uint8_t pp,i,m; + char h1; + char s1[2]; + GASData=250; + uint8_t count = 0; + + printf("\r\n Ԥ��8��... \r\n"); + while (1) + { + if (count > 32) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(800);//Ԥ��8�� + + printf("\r\n Ԥ�Ƚ���... \r\n"); + for(j = 0; j < 30; j++) + { + printf("\r\n ��ʼ����... \r\n"); + TestReceiveLength = gsdk_uart_read(g_huart, (uint8_t *)TestReceiveBuff, 300, 1000); + printf("\r\n TestReceiveLength is : %d \r\n", TestReceiveLength); + if(TestReceiveLength > 0) + { + pGasRecevBuff = Find_SpecialString(TestReceiveBuff+180, RecevFromGas, strlen(TestReceiveBuff), 4); + if(pGasRecevBuff != NULL) //��ָֹ��Խ�� + { + printf("\r\n pGasRecevBuff is : %s \r\n", pGasRecevBuff); + pRecevBuff = Find_SpecialString(pGasRecevBuff, RecevFromRadar, strlen(pGasRecevBuff), 2); + for(m = 0; m < 29; m++) + { + printf("\r\npRecevBuff---------- %.2x\r\n", pRecevBuff[m]); + } + if(pRecevBuff != NULL) //��ָֹ��Խ�� + { + if((pRecevBuff[9]==0x20)&&(pRecevBuff[15]==0x20)&&(pRecevBuff[23]==0x20)&&(pRecevBuff[26]==0x20)) + { + //printf("\r\nUART4�������ݽ���!!\r\n"); + pp= pRecevBuff[2]; + for(i=1; i<25; i++) + { + pp^= pRecevBuff[i+2]; + } + s1[0]= pRecevBuff[27]; + s1[1]= pRecevBuff[28]; + string_to_hex(&h1,s1); + + printf("\r\nUART4 У�� %.2x %.2x!!\r\n",pp,h1); + if(pp== h1) //��λ���У�� + { + if(pRecevBuff[24]==0x30) //�����ź� + { + printf("\r\npRecevBuff[25]---------------------------- %.2x!!\r\n",pRecevBuff[25]); + if(pRecevBuff[25]==0x30) //�����ź� + { + printf("\r\n ����״̬���� \r\n"); + if( pRecevBuff[6]==0x2e ) //С���� + { + GASData =(pRecevBuff[3]-0x30)*100 + (pRecevBuff[4]-0x30)*10 + (pRecevBuff[5]-0x30)*1.0 + (pRecevBuff[7]-0x30)*0.1 + (pRecevBuff[8]-0x30)*0.01 ; + if(pRecevBuff[2]==0x2b ) // ������ + { + + } + else if(pRecevBuff[2]==0x2d) // ������ + { + GASData=-GASData; + } + else + { + GASData =250;//ȼ��������ͨ���쳣 + } + } + } + else if(pRecevBuff[25]==0x31) + { + printf("\r\n ��·�쳣���޷�����ʹ�� \r\n"); + GASData =240; + } + else if(pRecevBuff[25]==0x32) // + { + printf("\r\n �¶�ѹ���쳣 \r\n"); + GASData =240; + } + } + else + { + printf("\r\n ȼ��̽ͷ�쳣 \r\n"); + } + } + } + } + } + } + if(GASData <=100) break; + while (1) + { + if (count > 2) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(100); + memset(TestReceiveBuff, 0x00, 300); + } + printf("\r\n GASData is : %d \r\n", (uint8_t)GASData); + + gsdk_gpio_write(hgpio, GPIO_DATA_LOW);//�ر�ȼ��̽ͷ��Դ + return GASData; +} \ No newline at end of file diff --git a/rtc.c b/rtc.c new file mode 100644 index 0000000..450f4c7 --- /dev/null +++ b/rtc.c @@ -0,0 +1,340 @@ + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "rtc.h" +#include "AiderProtocol.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:F:%s,L:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +extern struct DeviceSet DeviceConfig; + +static gsdk_handle_t rtc_handle; + +void rtc_alarm_cb(void *user_data) +{ + (void)user_data; + gsdk_ril_init(); + gsdk_ril_set_hardware_reboot();//����ģ�� + OEM_LOGI("[RTC_DEMO] rtc alarm handle cb------------------------0!\n"); +} + +void utc_to_bj_time(rtc_time_t rtc_time) +{ + int year,month,day,hour; + int lastday = 0; //last day of this month + int lastlastday = 0; //last day of last month + char temp_buf[100] = {0}; + + year = rtc_time.rtc_year + 2000; //utc time + month = rtc_time.rtc_mon; + day = rtc_time.rtc_day; + hour = rtc_time.rtc_hour + 8; + + if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) + { + lastday = 31; + lastlastday = 30;//����Ӧ������ϸ��µ����� + + if(month == 3) + { + if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year + lastlastday = 29; + else + lastlastday = 28; + } + + if(month == 8 || month == 1)//����Ӧ����8�º�1�£���Ϊ8�º�1�µ���һ���µ�������31��� + lastlastday = 31; + } + else if(month == 4 || month == 6 || month == 9 || month == 11) + { + lastday = 30; + lastlastday = 31; + } + else + { + lastlastday = 31; + + if((year%400 == 0)||(year%4 == 0 && year%100 != 0)) + lastday = 29; + else + lastday = 28; + } + + if(hour >= 24) // if >24, day+1 + { + hour -= 24; + day += 1; + + if(day > lastday) // next month, day-lastday of this month + { + day -= lastday; + month += 1; + + if(month > 12) // next year , month-12 + { + month -= 12; + year += 1; + } + } + } + if(hour < 0) // if <0, day-1 + { + hour += 24; + day -= 1; + if(day < 1) // month-1, day=last day of last month + { + day = lastlastday; + month -= 1; + if(month < 1) // last year , month=12 + { + month = 12; + year -= 1; + } + } + } + + sprintf(temp_buf, + "Beijing time,%4d,%02d,%02d,%02d,%02d,%02d", + year, + month, + day, + hour, + rtc_time.rtc_min, + rtc_time.rtc_sec); + + OEM_LOGI("��ǰʱ��: %s!\n", temp_buf); + + DeviceConfig.Time_Year = year-2000;/* ϵͳ���ڣ�OID��0x10000050 */ + DeviceConfig.Time_Mon = month; + DeviceConfig.Time_Mday = day; + DeviceConfig.Time_Hour = hour; + DeviceConfig.Time_Min = rtc_time.rtc_min; +} + +/******************************************************************************* +* Function Name : RTC_Init +* Description : RTC��ʼ������ȡϵͳʱ�� +* Input : struct DeviceSet* DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Init(void) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + status = gsdk_rtc_open(&rtc_handle); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_open failed!\n"); + goto _fail; + } + + status = gsdk_rtc_time_read(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_time_read failed!\n"); + goto _fail; + } + + utc_to_bj_time(rtc_time); + + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Init failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : RTC_Timing +* Description : RTCУʱ +* Input : uint8_t SetTime_Flag��1ʱ��ָ��������ã�2ʱ������Ϊ������Уʱʱ�䣩, struct DeviceSet DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Timing(uint8_t SetTime_Flag) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + if(SetTime_Flag == 1) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = 21; + rtc_time.rtc_mon = 1; + rtc_time.rtc_day = 1; + rtc_time.rtc_week = 1; + rtc_time.rtc_hour = 1; + rtc_time.rtc_min = 1; + rtc_time.rtc_sec = 1; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + else if(SetTime_Flag == 2) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = DeviceConfig.Time_Year; + rtc_time.rtc_mon = DeviceConfig.Time_Mon; + rtc_time.rtc_day = DeviceConfig.Time_Mday; + rtc_time.rtc_hour = DeviceConfig.Time_Hour; + rtc_time.rtc_min = DeviceConfig.Time_Min; + rtc_time.rtc_sec = DeviceConfig.Time_Sec; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Timing failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : Set_Alarm +* Description : RTC�������� +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : 1�ɹ���0ʧ�� +*******************************************************************************/ +void Set_Alarm(long SleepPara) +{ + gsdk_status_t status; + int i,j; + gsdk_handle_t g_alarm_handle; + uint8_t count = 0; + + if(SleepPara > 0) + { + /*configure alarm timer property*/ + gsdk_alarm_config_t alarm_config; + alarm_config.callback = rtc_alarm_cb;//timer callback + alarm_config.interval = SleepPara;//��λ��s + alarm_config.periodic = 1;//timer perform periodically + status = gsdk_alarm_open(&g_alarm_handle, &alarm_config);//open alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("open alarm failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("open alarm success\r\n"); + status = gsdk_alarm_start(g_alarm_handle);//start up alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("gsdk_alarm_start failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("start alarm success\r\n"); + } + gsdk_rtc_close(rtc_handle); + + for(i=0; i<3; i++) + { + status = gsdk_ril_psm_set(CPSMS, "AT+CPSMS=1");//set "CPSMS=1" is power saving mode(PSM) + if(GSDK_ERROR == status) + { + DEBUG_LOG("set CPSMS=1 cmd error\r\n"); + goto _fail; + } + WARN_LOG("set CPSM=1 cmd success\r\n"); + + status = gsdk_ril_psm_set(ZSLR, "AT+ZSLR");//sleep command + if(GSDK_ERROR == status) + { + DEBUG_LOG("set ZSLR cmd error\r\n"); + goto _fail; + } + + WARN_LOG("wait for sleep....\r\n"); + for(j=0; j<3; j++) + { + DEBUG_LOG("-->ME3616 IS SLEEP <--\r\n"); + while (1) + { + if (count > 20) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(1000); //��ʱ10S���ȴ�˯�� + DEBUG_LOG("-->ME3616 IS WAKE <--\r\n"); + } + } + + gsdk_ril_set_hardware_reboot(); + +_fail: + WARN_LOG("Set_Alarm fail....\r\n"); + gsdk_ril_reset_module(); +} + +/******************************************************************************* +* Function Name : Get_Boot_Mode +* Description : ��ȡNBģ������ģʽ +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : Boot_Mode_Flag +*******************************************************************************/ +uint8_t Get_Boot_Mode(void) +{ + gsdk_boot_mode_t boot_mode; + gsdk_rtc_wakeup_mode_t wakeup_mode; + uint8_t Boot_Mode_Flag = 0; + + /*get boot mode function ,because this demo maybe enter PSM */ + boot_mode = gsdk_sys_get_boot_mode(&wakeup_mode); + DEBUG_LOG("boot_mode is---------------------------------------------- :%d\r\n", boot_mode); + if (1 == boot_mode || 2 == boot_mode) + { +// printf("wakeup from deep sleep\r\n"); + switch (wakeup_mode) + { + case GSDK_BOOT_RTC_TC_WAKEUP: + WARN_LOG("it's awakened by RTC_TC\r\n"); + break; + case GSDK_BOOT_RTC_EINT_WAKEUP: + WARN_LOG("it's awakened by RTC_EINT\r\n"); + break; + case GSDK_BOOT_RTC_ALARM_WAKEUP: + WARN_LOG("it's awakened by ALARM_WAKEUP\r\n"); + Boot_Mode_Flag = 1; + break; + case GSDK_BOOT_POWERKEY_WAKEUP: + WARN_LOG("it's awakened by POWERKEY\r\n"); + break; + default: + WARN_LOG("unknown mode\r\n"); + break; + } + } + else if (5 == boot_mode || 6 == boot_mode) + { + Boot_Mode_Flag = 3; + WARN_LOG("GSDK_BOOT_WDT_SW_RESET\r\n"); + } + else + { + Boot_Mode_Flag = 2; + WARN_LOG("not enter deep sleep\r\n"); + } + return Boot_Mode_Flag; +} \ No newline at end of file diff --git a/rtc.h b/rtc.h new file mode 100644 index 0000000..63f5cdb --- /dev/null +++ b/rtc.h @@ -0,0 +1,18 @@ +#ifndef __RTC_H +#define __RTC_H + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" + +int RTC_Init(void); //��ʼ��RTC +int RTC_Timing(uint8_t SetTime_Flag); +void Set_Alarm(long SleepPara); +uint8_t Get_Boot_Mode(void); +void utc_to_bj_time(rtc_time_t rtc_time); + +#endif + + diff --git a/sleep.c b/sleep.c new file mode 100644 index 0000000..a1e64e6 --- /dev/null +++ b/sleep.c @@ -0,0 +1,26 @@ + +#include "string.h" +#include "rtc.h" +#include "sleep.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include +#include + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void gotoSleep(long SleepPara) +{ + gsdk_ril_set_cfun_mode(0); // ����ģ��ĵ绰����Ϊ0 - ��С���ܣ���ʱ����SIM��Ҳ�������� + + printf("\r\nSLEEP OK! %ld min later wakeup!!\r\n", SleepPara); + Set_Alarm(SleepPara*60); +} + + diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..416f609 --- /dev/null +++ b/main.c @@ -0,0 +1,424 @@ +/******************************************************************************* + * @file main.c + * @author casic 203 + * @version V1.0 + * @date 2019-06-14 + * @brief + * @attention +*********************************************************************************/ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "AiderProtocol.h" +#include "API.h" +#include "sleep.h" +#include "math.h" +#include "NB-IOT.h" +#include "flash.h" + +#include "gsdk_sdk.h" +#include +#include +#include "gsdk_api.h" + +struct DeviceSet DeviceConfig = {0x00}; //������Ϣ�ṹ�� +struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +struct BusinessData businessData; +struct DataFrame dataFrame; + +uint8_t DeviceID[] = DEVICE_ID; //��ʼ���豸ID�� +uint16_t NodeAddr =0x0000; +uint8_t RouteControl =0x03; //·�ɿ��Ʊ�����Ĭ��ʹ�����ݼ������������ݴ��� +uint8_t send_buff[300]= {'\0'}; +uint8_t history_send_buff[300]= {'\0'}; +double GASData=200; +uint16_t ACR; + +static gsdk_handle_t g_huart; +char TestReceiveBuff[300]; + +gsdk_handle_t hgpio; + +double Gas_DataGet(void); +extern uint8_t SIMCard_Type; + +uint8_t PD2_Flag = 0;//��ñ��־��1����203��ƽ̨������ + +int __io_puts(char *data, int len) +{ + int ret = 0; + if (g_huart) + { + ret = gsdk_uart_write(g_huart, (uint8_t *)data, len, 1000); + } + return ret; +} + +int log_init(void) +{ + uart_config_t uart_config; + gsdk_status_t status; + + uart_config.baudrate = UART_BAUDRATE_115200; + uart_config.parity = UART_PARITY_NONE; + uart_config.stop_bit = UART_STOP_BIT_1; + uart_config.word_length = UART_WORD_LENGTH_8; + + status = gsdk_uart_open(UART_0, &uart_config, &g_huart); + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[GPIO_DEMO]: failed to open uart %d\n", status); + return -1; + } + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t Gpio_Init(void) +{ + gsdk_status_t status; + gpio_config_t gpio_config; + gpio_pin_t gpio_pin; + + gpio_pin = GPIO_PIN_9; + + gpio_config.direction = GPIO_DIRECTION_OUTPUT; + gpio_config.pull = GPIO_PULL_NONE; + gpio_config.int_mode = GPIO_INT_DISABLE; + gpio_config.debounce_time = 0; + gpio_config.callback = NULL; + + status = gsdk_gpio_open(gpio_pin, &gpio_config, &hgpio); + if (status != GSDK_SUCCESS) + { + goto _fail; + } + + return 0; + +_fail: + gsdk_gpio_close(hgpio); + printf("[GPIO_DEMO] Gpio test failed!\n"); + return -1; +} + +void gpio_callback(void *user_data) +{ + printf("[GPIO_DEMO] gpio eint handle callback!\n"); +} + +/******************************************************************************* +* Function Name : void PeripheralInit(void) +* Description : ��ʼ���˿ڼ����� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t PeripheralInit(void) +{ + uint8_t Boot_Mode_Flag = 0; + uint8_t count = 0; + uint8_t RunCount = 0; + + Gpio_Init(); + log_init(); + gsdk_gpio_write(hgpio, GPIO_DATA_HIGH);//��ȼ��̽ͷ��Դ + Boot_Mode_Flag = Get_Boot_Mode(); + bsp_Init_DS2782(); + gsdk_ril_init(); + + Module_Flash_Read(RUN_COUNT_ADDR, &RunCount, 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (RunCount > 24) //�豸�����ⲿ��λ + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + else if (RunCount == 24) + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + RunCount++; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + printf("\n\r RunCount:%d \r\n", RunCount); + + gsdk_ril_set_ip_mode(IPV4); + gsdk_ril_set_cfun_mode(1); // ����ģ��ĵ绰����Ϊ1 - ȫ���� + while (1) + { + if (count > 6) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(300); + RTC_Init(); //��ȡϵͳ��ǰʱ�� + + printf("\n\r****************************************************************\r\n"); + printf("\n\r*-----��ӭʹ��BIRMM-GT200N ȼ�����ܼ��ܶ�!---------------*\r\n"); + printf("\n\r*-----����汾�ţ�%s ------------------------*\r\n", SOFTWARE_VERSION); + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + printf("\n\r*-----��Ȩ���У��������ߵ���������о���------------------*\r\n"); + printf("\n\r****************************************************************\r\n"); + + return Boot_Mode_Flag; +} + +/******************************************************************************* +* Function Name : int main(void) +* Description : ������ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int main(void) +{ + uint16_t Boot_Mode_Flag, StartupResponse_Flag; + + Boot_Mode_Flag = PeripheralInit(); //��ʼ������ + ConfigData_Init(); //��FLASH��ȡ���ò��� + +/* Module_DS2781_Set_Register(); + vTaskDelay(100); + + while(1) + { + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + printf("\r\n Get_DS2782_RAAC() is : %d \r\n", Get_DS2782_RAAC()); + printf("\r\n Get_DS2782_RSAC() is : %d \r\n", Get_DS2782_RSAC()); + printf("\r\n Get_DS2782_RSRC() is : %d \r\n", Get_DS2782_RSRC()); + + vTaskDelay(100); + }*/ + + //�����⵽���磬�ϴ����� + if(Boot_Mode_Flag == 2) + { +// RTC_Timing(1); + PD2_Flag = 1; + Module_DS2781_Set_Register(); + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + Code_Read_NB(); + StartupResponse_Flag = StartupRequest(send_buff,8);//������֡�ϴ� + if(StartupResponse_Flag==0x099C) printf("\r\n IMEI ICCID �ϱ��ɹ�����\r\n"); + else printf("\r\n IMEI ICCID �ϱ�ʧ�ܣ���\r\n"); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else if(Boot_Mode_Flag == 3) + { + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� + } + + if(businessData.SendCount>0) //������ʷ���� + { + Code_Read_NB(); + + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + SendMessage_NB_T(history_send_buff, businessData.HistoryDateCount); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + SendMessage_NB_MU(history_send_buff, businessData.HistoryDateCount); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + if(businessData.ParameterReply>0) //������ʷ���� + { + Code_Read_NB(); + StartupRequest(send_buff,12);//�������ûظ��ϴ� + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + + DeviceConfig.CollectPeriod = 1; + + /*����Ѳɼ����ݴ��ڲɼ�����*/ + if((businessData.DataCount >= DeviceConfig.CollectNum)) + { + Code_Read_NB(); + StartupResponse_Flag = TrapRequest(send_buff,4);//������֡�ϴ� + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + BusinessData_Collection(); + if(GASData <=100) + { + BusinessData_Storage(); + } + else{ + Code_Read_NB(); + TrapRequest(send_buff,4);//������֡�ϴ� + } + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� +} + +double Gas_DataGet(void) +{ + uint16_t TestReceiveLength = 0; + char *pRecevBuff = NULL; + char *pGasRecevBuff = NULL; + char RecevFromGas[4] = {0x20,0x30,0x30,0x20}; + char RecevFromRadar[2] = {0x0d,0x0a}; + uint8_t j = 0; + uint8_t pp,i,m; + char h1; + char s1[2]; + GASData=250; + uint8_t count = 0; + + printf("\r\n Ԥ��8��... \r\n"); + while (1) + { + if (count > 32) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(800);//Ԥ��8�� + + printf("\r\n Ԥ�Ƚ���... \r\n"); + for(j = 0; j < 30; j++) + { + printf("\r\n ��ʼ����... \r\n"); + TestReceiveLength = gsdk_uart_read(g_huart, (uint8_t *)TestReceiveBuff, 300, 1000); + printf("\r\n TestReceiveLength is : %d \r\n", TestReceiveLength); + if(TestReceiveLength > 0) + { + pGasRecevBuff = Find_SpecialString(TestReceiveBuff+180, RecevFromGas, strlen(TestReceiveBuff), 4); + if(pGasRecevBuff != NULL) //��ָֹ��Խ�� + { + printf("\r\n pGasRecevBuff is : %s \r\n", pGasRecevBuff); + pRecevBuff = Find_SpecialString(pGasRecevBuff, RecevFromRadar, strlen(pGasRecevBuff), 2); + for(m = 0; m < 29; m++) + { + printf("\r\npRecevBuff---------- %.2x\r\n", pRecevBuff[m]); + } + if(pRecevBuff != NULL) //��ָֹ��Խ�� + { + if((pRecevBuff[9]==0x20)&&(pRecevBuff[15]==0x20)&&(pRecevBuff[23]==0x20)&&(pRecevBuff[26]==0x20)) + { + //printf("\r\nUART4�������ݽ���!!\r\n"); + pp= pRecevBuff[2]; + for(i=1; i<25; i++) + { + pp^= pRecevBuff[i+2]; + } + s1[0]= pRecevBuff[27]; + s1[1]= pRecevBuff[28]; + string_to_hex(&h1,s1); + + printf("\r\nUART4 У�� %.2x %.2x!!\r\n",pp,h1); + if(pp== h1) //��λ���У�� + { + if(pRecevBuff[24]==0x30) //�����ź� + { + printf("\r\npRecevBuff[25]---------------------------- %.2x!!\r\n",pRecevBuff[25]); + if(pRecevBuff[25]==0x30) //�����ź� + { + printf("\r\n ����״̬���� \r\n"); + if( pRecevBuff[6]==0x2e ) //С���� + { + GASData =(pRecevBuff[3]-0x30)*100 + (pRecevBuff[4]-0x30)*10 + (pRecevBuff[5]-0x30)*1.0 + (pRecevBuff[7]-0x30)*0.1 + (pRecevBuff[8]-0x30)*0.01 ; + if(pRecevBuff[2]==0x2b ) // ������ + { + + } + else if(pRecevBuff[2]==0x2d) // ������ + { + GASData=-GASData; + } + else + { + GASData =250;//ȼ��������ͨ���쳣 + } + } + } + else if(pRecevBuff[25]==0x31) + { + printf("\r\n ��·�쳣���޷�����ʹ�� \r\n"); + GASData =240; + } + else if(pRecevBuff[25]==0x32) // + { + printf("\r\n �¶�ѹ���쳣 \r\n"); + GASData =240; + } + } + else + { + printf("\r\n ȼ��̽ͷ�쳣 \r\n"); + } + } + } + } + } + } + if(GASData <=100) break; + while (1) + { + if (count > 2) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(100); + memset(TestReceiveBuff, 0x00, 300); + } + printf("\r\n GASData is : %d \r\n", (uint8_t)GASData); + + gsdk_gpio_write(hgpio, GPIO_DATA_LOW);//�ر�ȼ��̽ͷ��Դ + return GASData; +} \ No newline at end of file diff --git a/rtc.c b/rtc.c new file mode 100644 index 0000000..450f4c7 --- /dev/null +++ b/rtc.c @@ -0,0 +1,340 @@ + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "rtc.h" +#include "AiderProtocol.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:F:%s,L:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +extern struct DeviceSet DeviceConfig; + +static gsdk_handle_t rtc_handle; + +void rtc_alarm_cb(void *user_data) +{ + (void)user_data; + gsdk_ril_init(); + gsdk_ril_set_hardware_reboot();//����ģ�� + OEM_LOGI("[RTC_DEMO] rtc alarm handle cb------------------------0!\n"); +} + +void utc_to_bj_time(rtc_time_t rtc_time) +{ + int year,month,day,hour; + int lastday = 0; //last day of this month + int lastlastday = 0; //last day of last month + char temp_buf[100] = {0}; + + year = rtc_time.rtc_year + 2000; //utc time + month = rtc_time.rtc_mon; + day = rtc_time.rtc_day; + hour = rtc_time.rtc_hour + 8; + + if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) + { + lastday = 31; + lastlastday = 30;//����Ӧ������ϸ��µ����� + + if(month == 3) + { + if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year + lastlastday = 29; + else + lastlastday = 28; + } + + if(month == 8 || month == 1)//����Ӧ����8�º�1�£���Ϊ8�º�1�µ���һ���µ�������31��� + lastlastday = 31; + } + else if(month == 4 || month == 6 || month == 9 || month == 11) + { + lastday = 30; + lastlastday = 31; + } + else + { + lastlastday = 31; + + if((year%400 == 0)||(year%4 == 0 && year%100 != 0)) + lastday = 29; + else + lastday = 28; + } + + if(hour >= 24) // if >24, day+1 + { + hour -= 24; + day += 1; + + if(day > lastday) // next month, day-lastday of this month + { + day -= lastday; + month += 1; + + if(month > 12) // next year , month-12 + { + month -= 12; + year += 1; + } + } + } + if(hour < 0) // if <0, day-1 + { + hour += 24; + day -= 1; + if(day < 1) // month-1, day=last day of last month + { + day = lastlastday; + month -= 1; + if(month < 1) // last year , month=12 + { + month = 12; + year -= 1; + } + } + } + + sprintf(temp_buf, + "Beijing time,%4d,%02d,%02d,%02d,%02d,%02d", + year, + month, + day, + hour, + rtc_time.rtc_min, + rtc_time.rtc_sec); + + OEM_LOGI("��ǰʱ��: %s!\n", temp_buf); + + DeviceConfig.Time_Year = year-2000;/* ϵͳ���ڣ�OID��0x10000050 */ + DeviceConfig.Time_Mon = month; + DeviceConfig.Time_Mday = day; + DeviceConfig.Time_Hour = hour; + DeviceConfig.Time_Min = rtc_time.rtc_min; +} + +/******************************************************************************* +* Function Name : RTC_Init +* Description : RTC��ʼ������ȡϵͳʱ�� +* Input : struct DeviceSet* DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Init(void) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + status = gsdk_rtc_open(&rtc_handle); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_open failed!\n"); + goto _fail; + } + + status = gsdk_rtc_time_read(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_time_read failed!\n"); + goto _fail; + } + + utc_to_bj_time(rtc_time); + + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Init failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : RTC_Timing +* Description : RTCУʱ +* Input : uint8_t SetTime_Flag��1ʱ��ָ��������ã�2ʱ������Ϊ������Уʱʱ�䣩, struct DeviceSet DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Timing(uint8_t SetTime_Flag) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + if(SetTime_Flag == 1) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = 21; + rtc_time.rtc_mon = 1; + rtc_time.rtc_day = 1; + rtc_time.rtc_week = 1; + rtc_time.rtc_hour = 1; + rtc_time.rtc_min = 1; + rtc_time.rtc_sec = 1; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + else if(SetTime_Flag == 2) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = DeviceConfig.Time_Year; + rtc_time.rtc_mon = DeviceConfig.Time_Mon; + rtc_time.rtc_day = DeviceConfig.Time_Mday; + rtc_time.rtc_hour = DeviceConfig.Time_Hour; + rtc_time.rtc_min = DeviceConfig.Time_Min; + rtc_time.rtc_sec = DeviceConfig.Time_Sec; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Timing failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : Set_Alarm +* Description : RTC�������� +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : 1�ɹ���0ʧ�� +*******************************************************************************/ +void Set_Alarm(long SleepPara) +{ + gsdk_status_t status; + int i,j; + gsdk_handle_t g_alarm_handle; + uint8_t count = 0; + + if(SleepPara > 0) + { + /*configure alarm timer property*/ + gsdk_alarm_config_t alarm_config; + alarm_config.callback = rtc_alarm_cb;//timer callback + alarm_config.interval = SleepPara;//��λ��s + alarm_config.periodic = 1;//timer perform periodically + status = gsdk_alarm_open(&g_alarm_handle, &alarm_config);//open alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("open alarm failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("open alarm success\r\n"); + status = gsdk_alarm_start(g_alarm_handle);//start up alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("gsdk_alarm_start failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("start alarm success\r\n"); + } + gsdk_rtc_close(rtc_handle); + + for(i=0; i<3; i++) + { + status = gsdk_ril_psm_set(CPSMS, "AT+CPSMS=1");//set "CPSMS=1" is power saving mode(PSM) + if(GSDK_ERROR == status) + { + DEBUG_LOG("set CPSMS=1 cmd error\r\n"); + goto _fail; + } + WARN_LOG("set CPSM=1 cmd success\r\n"); + + status = gsdk_ril_psm_set(ZSLR, "AT+ZSLR");//sleep command + if(GSDK_ERROR == status) + { + DEBUG_LOG("set ZSLR cmd error\r\n"); + goto _fail; + } + + WARN_LOG("wait for sleep....\r\n"); + for(j=0; j<3; j++) + { + DEBUG_LOG("-->ME3616 IS SLEEP <--\r\n"); + while (1) + { + if (count > 20) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(1000); //��ʱ10S���ȴ�˯�� + DEBUG_LOG("-->ME3616 IS WAKE <--\r\n"); + } + } + + gsdk_ril_set_hardware_reboot(); + +_fail: + WARN_LOG("Set_Alarm fail....\r\n"); + gsdk_ril_reset_module(); +} + +/******************************************************************************* +* Function Name : Get_Boot_Mode +* Description : ��ȡNBģ������ģʽ +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : Boot_Mode_Flag +*******************************************************************************/ +uint8_t Get_Boot_Mode(void) +{ + gsdk_boot_mode_t boot_mode; + gsdk_rtc_wakeup_mode_t wakeup_mode; + uint8_t Boot_Mode_Flag = 0; + + /*get boot mode function ,because this demo maybe enter PSM */ + boot_mode = gsdk_sys_get_boot_mode(&wakeup_mode); + DEBUG_LOG("boot_mode is---------------------------------------------- :%d\r\n", boot_mode); + if (1 == boot_mode || 2 == boot_mode) + { +// printf("wakeup from deep sleep\r\n"); + switch (wakeup_mode) + { + case GSDK_BOOT_RTC_TC_WAKEUP: + WARN_LOG("it's awakened by RTC_TC\r\n"); + break; + case GSDK_BOOT_RTC_EINT_WAKEUP: + WARN_LOG("it's awakened by RTC_EINT\r\n"); + break; + case GSDK_BOOT_RTC_ALARM_WAKEUP: + WARN_LOG("it's awakened by ALARM_WAKEUP\r\n"); + Boot_Mode_Flag = 1; + break; + case GSDK_BOOT_POWERKEY_WAKEUP: + WARN_LOG("it's awakened by POWERKEY\r\n"); + break; + default: + WARN_LOG("unknown mode\r\n"); + break; + } + } + else if (5 == boot_mode || 6 == boot_mode) + { + Boot_Mode_Flag = 3; + WARN_LOG("GSDK_BOOT_WDT_SW_RESET\r\n"); + } + else + { + Boot_Mode_Flag = 2; + WARN_LOG("not enter deep sleep\r\n"); + } + return Boot_Mode_Flag; +} \ No newline at end of file diff --git a/rtc.h b/rtc.h new file mode 100644 index 0000000..63f5cdb --- /dev/null +++ b/rtc.h @@ -0,0 +1,18 @@ +#ifndef __RTC_H +#define __RTC_H + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" + +int RTC_Init(void); //��ʼ��RTC +int RTC_Timing(uint8_t SetTime_Flag); +void Set_Alarm(long SleepPara); +uint8_t Get_Boot_Mode(void); +void utc_to_bj_time(rtc_time_t rtc_time); + +#endif + + diff --git a/sleep.c b/sleep.c new file mode 100644 index 0000000..a1e64e6 --- /dev/null +++ b/sleep.c @@ -0,0 +1,26 @@ + +#include "string.h" +#include "rtc.h" +#include "sleep.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include +#include + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void gotoSleep(long SleepPara) +{ + gsdk_ril_set_cfun_mode(0); // ����ģ��ĵ绰����Ϊ0 - ��С���ܣ���ʱ����SIM��Ҳ�������� + + printf("\r\nSLEEP OK! %ld min later wakeup!!\r\n", SleepPara); + Set_Alarm(SleepPara*60); +} + + diff --git a/sleep.h b/sleep.h new file mode 100644 index 0000000..d18c287 --- /dev/null +++ b/sleep.h @@ -0,0 +1,7 @@ +#ifndef _SLEEP_H_ +#define _SLEEP_H_ + +void gotoSleep(long SleepPara); + +#endif + diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..416f609 --- /dev/null +++ b/main.c @@ -0,0 +1,424 @@ +/******************************************************************************* + * @file main.c + * @author casic 203 + * @version V1.0 + * @date 2019-06-14 + * @brief + * @attention +*********************************************************************************/ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "AiderProtocol.h" +#include "API.h" +#include "sleep.h" +#include "math.h" +#include "NB-IOT.h" +#include "flash.h" + +#include "gsdk_sdk.h" +#include +#include +#include "gsdk_api.h" + +struct DeviceSet DeviceConfig = {0x00}; //������Ϣ�ṹ�� +struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +struct BusinessData businessData; +struct DataFrame dataFrame; + +uint8_t DeviceID[] = DEVICE_ID; //��ʼ���豸ID�� +uint16_t NodeAddr =0x0000; +uint8_t RouteControl =0x03; //·�ɿ��Ʊ�����Ĭ��ʹ�����ݼ������������ݴ��� +uint8_t send_buff[300]= {'\0'}; +uint8_t history_send_buff[300]= {'\0'}; +double GASData=200; +uint16_t ACR; + +static gsdk_handle_t g_huart; +char TestReceiveBuff[300]; + +gsdk_handle_t hgpio; + +double Gas_DataGet(void); +extern uint8_t SIMCard_Type; + +uint8_t PD2_Flag = 0;//��ñ��־��1����203��ƽ̨������ + +int __io_puts(char *data, int len) +{ + int ret = 0; + if (g_huart) + { + ret = gsdk_uart_write(g_huart, (uint8_t *)data, len, 1000); + } + return ret; +} + +int log_init(void) +{ + uart_config_t uart_config; + gsdk_status_t status; + + uart_config.baudrate = UART_BAUDRATE_115200; + uart_config.parity = UART_PARITY_NONE; + uart_config.stop_bit = UART_STOP_BIT_1; + uart_config.word_length = UART_WORD_LENGTH_8; + + status = gsdk_uart_open(UART_0, &uart_config, &g_huart); + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[GPIO_DEMO]: failed to open uart %d\n", status); + return -1; + } + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t Gpio_Init(void) +{ + gsdk_status_t status; + gpio_config_t gpio_config; + gpio_pin_t gpio_pin; + + gpio_pin = GPIO_PIN_9; + + gpio_config.direction = GPIO_DIRECTION_OUTPUT; + gpio_config.pull = GPIO_PULL_NONE; + gpio_config.int_mode = GPIO_INT_DISABLE; + gpio_config.debounce_time = 0; + gpio_config.callback = NULL; + + status = gsdk_gpio_open(gpio_pin, &gpio_config, &hgpio); + if (status != GSDK_SUCCESS) + { + goto _fail; + } + + return 0; + +_fail: + gsdk_gpio_close(hgpio); + printf("[GPIO_DEMO] Gpio test failed!\n"); + return -1; +} + +void gpio_callback(void *user_data) +{ + printf("[GPIO_DEMO] gpio eint handle callback!\n"); +} + +/******************************************************************************* +* Function Name : void PeripheralInit(void) +* Description : ��ʼ���˿ڼ����� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t PeripheralInit(void) +{ + uint8_t Boot_Mode_Flag = 0; + uint8_t count = 0; + uint8_t RunCount = 0; + + Gpio_Init(); + log_init(); + gsdk_gpio_write(hgpio, GPIO_DATA_HIGH);//��ȼ��̽ͷ��Դ + Boot_Mode_Flag = Get_Boot_Mode(); + bsp_Init_DS2782(); + gsdk_ril_init(); + + Module_Flash_Read(RUN_COUNT_ADDR, &RunCount, 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (RunCount > 24) //�豸�����ⲿ��λ + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + else if (RunCount == 24) + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + RunCount++; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + printf("\n\r RunCount:%d \r\n", RunCount); + + gsdk_ril_set_ip_mode(IPV4); + gsdk_ril_set_cfun_mode(1); // ����ģ��ĵ绰����Ϊ1 - ȫ���� + while (1) + { + if (count > 6) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(300); + RTC_Init(); //��ȡϵͳ��ǰʱ�� + + printf("\n\r****************************************************************\r\n"); + printf("\n\r*-----��ӭʹ��BIRMM-GT200N ȼ�����ܼ��ܶ�!---------------*\r\n"); + printf("\n\r*-----����汾�ţ�%s ------------------------*\r\n", SOFTWARE_VERSION); + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + printf("\n\r*-----��Ȩ���У��������ߵ���������о���------------------*\r\n"); + printf("\n\r****************************************************************\r\n"); + + return Boot_Mode_Flag; +} + +/******************************************************************************* +* Function Name : int main(void) +* Description : ������ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int main(void) +{ + uint16_t Boot_Mode_Flag, StartupResponse_Flag; + + Boot_Mode_Flag = PeripheralInit(); //��ʼ������ + ConfigData_Init(); //��FLASH��ȡ���ò��� + +/* Module_DS2781_Set_Register(); + vTaskDelay(100); + + while(1) + { + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + printf("\r\n Get_DS2782_RAAC() is : %d \r\n", Get_DS2782_RAAC()); + printf("\r\n Get_DS2782_RSAC() is : %d \r\n", Get_DS2782_RSAC()); + printf("\r\n Get_DS2782_RSRC() is : %d \r\n", Get_DS2782_RSRC()); + + vTaskDelay(100); + }*/ + + //�����⵽���磬�ϴ����� + if(Boot_Mode_Flag == 2) + { +// RTC_Timing(1); + PD2_Flag = 1; + Module_DS2781_Set_Register(); + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + Code_Read_NB(); + StartupResponse_Flag = StartupRequest(send_buff,8);//������֡�ϴ� + if(StartupResponse_Flag==0x099C) printf("\r\n IMEI ICCID �ϱ��ɹ�����\r\n"); + else printf("\r\n IMEI ICCID �ϱ�ʧ�ܣ���\r\n"); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else if(Boot_Mode_Flag == 3) + { + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� + } + + if(businessData.SendCount>0) //������ʷ���� + { + Code_Read_NB(); + + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + SendMessage_NB_T(history_send_buff, businessData.HistoryDateCount); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + SendMessage_NB_MU(history_send_buff, businessData.HistoryDateCount); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + if(businessData.ParameterReply>0) //������ʷ���� + { + Code_Read_NB(); + StartupRequest(send_buff,12);//�������ûظ��ϴ� + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + + DeviceConfig.CollectPeriod = 1; + + /*����Ѳɼ����ݴ��ڲɼ�����*/ + if((businessData.DataCount >= DeviceConfig.CollectNum)) + { + Code_Read_NB(); + StartupResponse_Flag = TrapRequest(send_buff,4);//������֡�ϴ� + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + BusinessData_Collection(); + if(GASData <=100) + { + BusinessData_Storage(); + } + else{ + Code_Read_NB(); + TrapRequest(send_buff,4);//������֡�ϴ� + } + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� +} + +double Gas_DataGet(void) +{ + uint16_t TestReceiveLength = 0; + char *pRecevBuff = NULL; + char *pGasRecevBuff = NULL; + char RecevFromGas[4] = {0x20,0x30,0x30,0x20}; + char RecevFromRadar[2] = {0x0d,0x0a}; + uint8_t j = 0; + uint8_t pp,i,m; + char h1; + char s1[2]; + GASData=250; + uint8_t count = 0; + + printf("\r\n Ԥ��8��... \r\n"); + while (1) + { + if (count > 32) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(800);//Ԥ��8�� + + printf("\r\n Ԥ�Ƚ���... \r\n"); + for(j = 0; j < 30; j++) + { + printf("\r\n ��ʼ����... \r\n"); + TestReceiveLength = gsdk_uart_read(g_huart, (uint8_t *)TestReceiveBuff, 300, 1000); + printf("\r\n TestReceiveLength is : %d \r\n", TestReceiveLength); + if(TestReceiveLength > 0) + { + pGasRecevBuff = Find_SpecialString(TestReceiveBuff+180, RecevFromGas, strlen(TestReceiveBuff), 4); + if(pGasRecevBuff != NULL) //��ָֹ��Խ�� + { + printf("\r\n pGasRecevBuff is : %s \r\n", pGasRecevBuff); + pRecevBuff = Find_SpecialString(pGasRecevBuff, RecevFromRadar, strlen(pGasRecevBuff), 2); + for(m = 0; m < 29; m++) + { + printf("\r\npRecevBuff---------- %.2x\r\n", pRecevBuff[m]); + } + if(pRecevBuff != NULL) //��ָֹ��Խ�� + { + if((pRecevBuff[9]==0x20)&&(pRecevBuff[15]==0x20)&&(pRecevBuff[23]==0x20)&&(pRecevBuff[26]==0x20)) + { + //printf("\r\nUART4�������ݽ���!!\r\n"); + pp= pRecevBuff[2]; + for(i=1; i<25; i++) + { + pp^= pRecevBuff[i+2]; + } + s1[0]= pRecevBuff[27]; + s1[1]= pRecevBuff[28]; + string_to_hex(&h1,s1); + + printf("\r\nUART4 У�� %.2x %.2x!!\r\n",pp,h1); + if(pp== h1) //��λ���У�� + { + if(pRecevBuff[24]==0x30) //�����ź� + { + printf("\r\npRecevBuff[25]---------------------------- %.2x!!\r\n",pRecevBuff[25]); + if(pRecevBuff[25]==0x30) //�����ź� + { + printf("\r\n ����״̬���� \r\n"); + if( pRecevBuff[6]==0x2e ) //С���� + { + GASData =(pRecevBuff[3]-0x30)*100 + (pRecevBuff[4]-0x30)*10 + (pRecevBuff[5]-0x30)*1.0 + (pRecevBuff[7]-0x30)*0.1 + (pRecevBuff[8]-0x30)*0.01 ; + if(pRecevBuff[2]==0x2b ) // ������ + { + + } + else if(pRecevBuff[2]==0x2d) // ������ + { + GASData=-GASData; + } + else + { + GASData =250;//ȼ��������ͨ���쳣 + } + } + } + else if(pRecevBuff[25]==0x31) + { + printf("\r\n ��·�쳣���޷�����ʹ�� \r\n"); + GASData =240; + } + else if(pRecevBuff[25]==0x32) // + { + printf("\r\n �¶�ѹ���쳣 \r\n"); + GASData =240; + } + } + else + { + printf("\r\n ȼ��̽ͷ�쳣 \r\n"); + } + } + } + } + } + } + if(GASData <=100) break; + while (1) + { + if (count > 2) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(100); + memset(TestReceiveBuff, 0x00, 300); + } + printf("\r\n GASData is : %d \r\n", (uint8_t)GASData); + + gsdk_gpio_write(hgpio, GPIO_DATA_LOW);//�ر�ȼ��̽ͷ��Դ + return GASData; +} \ No newline at end of file diff --git a/rtc.c b/rtc.c new file mode 100644 index 0000000..450f4c7 --- /dev/null +++ b/rtc.c @@ -0,0 +1,340 @@ + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "rtc.h" +#include "AiderProtocol.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:F:%s,L:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +extern struct DeviceSet DeviceConfig; + +static gsdk_handle_t rtc_handle; + +void rtc_alarm_cb(void *user_data) +{ + (void)user_data; + gsdk_ril_init(); + gsdk_ril_set_hardware_reboot();//����ģ�� + OEM_LOGI("[RTC_DEMO] rtc alarm handle cb------------------------0!\n"); +} + +void utc_to_bj_time(rtc_time_t rtc_time) +{ + int year,month,day,hour; + int lastday = 0; //last day of this month + int lastlastday = 0; //last day of last month + char temp_buf[100] = {0}; + + year = rtc_time.rtc_year + 2000; //utc time + month = rtc_time.rtc_mon; + day = rtc_time.rtc_day; + hour = rtc_time.rtc_hour + 8; + + if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) + { + lastday = 31; + lastlastday = 30;//����Ӧ������ϸ��µ����� + + if(month == 3) + { + if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year + lastlastday = 29; + else + lastlastday = 28; + } + + if(month == 8 || month == 1)//����Ӧ����8�º�1�£���Ϊ8�º�1�µ���һ���µ�������31��� + lastlastday = 31; + } + else if(month == 4 || month == 6 || month == 9 || month == 11) + { + lastday = 30; + lastlastday = 31; + } + else + { + lastlastday = 31; + + if((year%400 == 0)||(year%4 == 0 && year%100 != 0)) + lastday = 29; + else + lastday = 28; + } + + if(hour >= 24) // if >24, day+1 + { + hour -= 24; + day += 1; + + if(day > lastday) // next month, day-lastday of this month + { + day -= lastday; + month += 1; + + if(month > 12) // next year , month-12 + { + month -= 12; + year += 1; + } + } + } + if(hour < 0) // if <0, day-1 + { + hour += 24; + day -= 1; + if(day < 1) // month-1, day=last day of last month + { + day = lastlastday; + month -= 1; + if(month < 1) // last year , month=12 + { + month = 12; + year -= 1; + } + } + } + + sprintf(temp_buf, + "Beijing time,%4d,%02d,%02d,%02d,%02d,%02d", + year, + month, + day, + hour, + rtc_time.rtc_min, + rtc_time.rtc_sec); + + OEM_LOGI("��ǰʱ��: %s!\n", temp_buf); + + DeviceConfig.Time_Year = year-2000;/* ϵͳ���ڣ�OID��0x10000050 */ + DeviceConfig.Time_Mon = month; + DeviceConfig.Time_Mday = day; + DeviceConfig.Time_Hour = hour; + DeviceConfig.Time_Min = rtc_time.rtc_min; +} + +/******************************************************************************* +* Function Name : RTC_Init +* Description : RTC��ʼ������ȡϵͳʱ�� +* Input : struct DeviceSet* DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Init(void) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + status = gsdk_rtc_open(&rtc_handle); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_open failed!\n"); + goto _fail; + } + + status = gsdk_rtc_time_read(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_time_read failed!\n"); + goto _fail; + } + + utc_to_bj_time(rtc_time); + + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Init failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : RTC_Timing +* Description : RTCУʱ +* Input : uint8_t SetTime_Flag��1ʱ��ָ��������ã�2ʱ������Ϊ������Уʱʱ�䣩, struct DeviceSet DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Timing(uint8_t SetTime_Flag) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + if(SetTime_Flag == 1) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = 21; + rtc_time.rtc_mon = 1; + rtc_time.rtc_day = 1; + rtc_time.rtc_week = 1; + rtc_time.rtc_hour = 1; + rtc_time.rtc_min = 1; + rtc_time.rtc_sec = 1; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + else if(SetTime_Flag == 2) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = DeviceConfig.Time_Year; + rtc_time.rtc_mon = DeviceConfig.Time_Mon; + rtc_time.rtc_day = DeviceConfig.Time_Mday; + rtc_time.rtc_hour = DeviceConfig.Time_Hour; + rtc_time.rtc_min = DeviceConfig.Time_Min; + rtc_time.rtc_sec = DeviceConfig.Time_Sec; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Timing failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : Set_Alarm +* Description : RTC�������� +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : 1�ɹ���0ʧ�� +*******************************************************************************/ +void Set_Alarm(long SleepPara) +{ + gsdk_status_t status; + int i,j; + gsdk_handle_t g_alarm_handle; + uint8_t count = 0; + + if(SleepPara > 0) + { + /*configure alarm timer property*/ + gsdk_alarm_config_t alarm_config; + alarm_config.callback = rtc_alarm_cb;//timer callback + alarm_config.interval = SleepPara;//��λ��s + alarm_config.periodic = 1;//timer perform periodically + status = gsdk_alarm_open(&g_alarm_handle, &alarm_config);//open alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("open alarm failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("open alarm success\r\n"); + status = gsdk_alarm_start(g_alarm_handle);//start up alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("gsdk_alarm_start failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("start alarm success\r\n"); + } + gsdk_rtc_close(rtc_handle); + + for(i=0; i<3; i++) + { + status = gsdk_ril_psm_set(CPSMS, "AT+CPSMS=1");//set "CPSMS=1" is power saving mode(PSM) + if(GSDK_ERROR == status) + { + DEBUG_LOG("set CPSMS=1 cmd error\r\n"); + goto _fail; + } + WARN_LOG("set CPSM=1 cmd success\r\n"); + + status = gsdk_ril_psm_set(ZSLR, "AT+ZSLR");//sleep command + if(GSDK_ERROR == status) + { + DEBUG_LOG("set ZSLR cmd error\r\n"); + goto _fail; + } + + WARN_LOG("wait for sleep....\r\n"); + for(j=0; j<3; j++) + { + DEBUG_LOG("-->ME3616 IS SLEEP <--\r\n"); + while (1) + { + if (count > 20) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(1000); //��ʱ10S���ȴ�˯�� + DEBUG_LOG("-->ME3616 IS WAKE <--\r\n"); + } + } + + gsdk_ril_set_hardware_reboot(); + +_fail: + WARN_LOG("Set_Alarm fail....\r\n"); + gsdk_ril_reset_module(); +} + +/******************************************************************************* +* Function Name : Get_Boot_Mode +* Description : ��ȡNBģ������ģʽ +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : Boot_Mode_Flag +*******************************************************************************/ +uint8_t Get_Boot_Mode(void) +{ + gsdk_boot_mode_t boot_mode; + gsdk_rtc_wakeup_mode_t wakeup_mode; + uint8_t Boot_Mode_Flag = 0; + + /*get boot mode function ,because this demo maybe enter PSM */ + boot_mode = gsdk_sys_get_boot_mode(&wakeup_mode); + DEBUG_LOG("boot_mode is---------------------------------------------- :%d\r\n", boot_mode); + if (1 == boot_mode || 2 == boot_mode) + { +// printf("wakeup from deep sleep\r\n"); + switch (wakeup_mode) + { + case GSDK_BOOT_RTC_TC_WAKEUP: + WARN_LOG("it's awakened by RTC_TC\r\n"); + break; + case GSDK_BOOT_RTC_EINT_WAKEUP: + WARN_LOG("it's awakened by RTC_EINT\r\n"); + break; + case GSDK_BOOT_RTC_ALARM_WAKEUP: + WARN_LOG("it's awakened by ALARM_WAKEUP\r\n"); + Boot_Mode_Flag = 1; + break; + case GSDK_BOOT_POWERKEY_WAKEUP: + WARN_LOG("it's awakened by POWERKEY\r\n"); + break; + default: + WARN_LOG("unknown mode\r\n"); + break; + } + } + else if (5 == boot_mode || 6 == boot_mode) + { + Boot_Mode_Flag = 3; + WARN_LOG("GSDK_BOOT_WDT_SW_RESET\r\n"); + } + else + { + Boot_Mode_Flag = 2; + WARN_LOG("not enter deep sleep\r\n"); + } + return Boot_Mode_Flag; +} \ No newline at end of file diff --git a/rtc.h b/rtc.h new file mode 100644 index 0000000..63f5cdb --- /dev/null +++ b/rtc.h @@ -0,0 +1,18 @@ +#ifndef __RTC_H +#define __RTC_H + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" + +int RTC_Init(void); //��ʼ��RTC +int RTC_Timing(uint8_t SetTime_Flag); +void Set_Alarm(long SleepPara); +uint8_t Get_Boot_Mode(void); +void utc_to_bj_time(rtc_time_t rtc_time); + +#endif + + diff --git a/sleep.c b/sleep.c new file mode 100644 index 0000000..a1e64e6 --- /dev/null +++ b/sleep.c @@ -0,0 +1,26 @@ + +#include "string.h" +#include "rtc.h" +#include "sleep.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include +#include + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void gotoSleep(long SleepPara) +{ + gsdk_ril_set_cfun_mode(0); // ����ģ��ĵ绰����Ϊ0 - ��С���ܣ���ʱ����SIM��Ҳ�������� + + printf("\r\nSLEEP OK! %ld min later wakeup!!\r\n", SleepPara); + Set_Alarm(SleepPara*60); +} + + diff --git a/sleep.h b/sleep.h new file mode 100644 index 0000000..d18c287 --- /dev/null +++ b/sleep.h @@ -0,0 +1,7 @@ +#ifndef _SLEEP_H_ +#define _SLEEP_H_ + +void gotoSleep(long SleepPara); + +#endif + diff --git a/~WRL0003.tmp b/~WRL0003.tmp new file mode 100644 index 0000000..a37dbb4 --- /dev/null +++ b/~WRL0003.tmp Binary files differ diff --git a/API-Platform.c b/API-Platform.c new file mode 100644 index 0000000..4c46e39 --- /dev/null +++ b/API-Platform.c @@ -0,0 +1,48 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.C +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-Platform.h" + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ) +{ + return ( iValue >> 0X08 ) + ( ( iValue & 0XFF ) << 0X08 ); +} + +// ***************************************************************************** +// Design Notes: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ) +{ + unsigned short iHVal; + unsigned short iLVal; + + // The MSB part + iHVal = ntohs( dwValue >> 0X10 ); + + // The LSB part + iLVal = ntohs( dwValue & 0XFFFF ); + + // Reverse the high and low part + return ( ( unsigned long )iLVal << 0x10 ) + iHVal; +} diff --git a/API-Platform.h b/API-Platform.h new file mode 100644 index 0000000..73e77d5 --- /dev/null +++ b/API-Platform.h @@ -0,0 +1,333 @@ +// ############################################################################# +// ***************************************************************************** +// Copyright (c) 2007-2008, WiMi-net (Beijing) Tech. Co., Ltd. +// THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY +// INFORMATION WHICH IS THE PROPERTY OF WIMI-NET TECH. CO., LTD. +// +// ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM +// WIMI-NET TECH. CO., LTD, IS STRICTLY PROHIBITED. +// ***************************************************************************** +// ############################################################################# +// +// File: API-Platform.h +// Author: Mickle.ding +// Created: 5/18/2007 +// +// Description: +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef _API_PLATFORM_INC_ + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define _API_PLATFORM_INC_ + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define LITTLE_ENDIAN_MODE 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define BIG_ENDIAN_MODE 0X01 + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#ifndef DEBUG + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG 0X00 + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_PORT ( DEBUG & 0X7F ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define DEBUG_OPEN ( DEBUG & 0X80 ) + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if ( DEBUG_PORT == 0X01 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X02 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-UART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X03 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X04 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X05 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-USART2.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutUSART2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X06 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART0.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X07 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#include "API-LEUART1.h" + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) PutLEUART1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X08 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket0String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X09 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket1String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0A ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket2String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif ( DEBUG_PORT == 0X0B ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) WriteSocket3String( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#else + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define PutString( X ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE LITTLE_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __C51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#elif defined ( __CX51__ ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CPU_ENDIAN_MODE BIG_ENDIAN_MODE + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define XDATA xdata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define IDATA idata + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#define CODE code + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned short ntohs( unsigned short iValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +unsigned long ntohl( unsigned long dwValue ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +void debugthread( char * pThreadName, unsigned char iStatus ); + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif diff --git a/API.c b/API.c new file mode 100644 index 0000000..b271f51 --- /dev/null +++ b/API.c @@ -0,0 +1,278 @@ +#include "API.h" +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "flash.h" + +extern struct DeviceSet DeviceConfig; +extern struct BusinessData businessData; +extern uint8_t history_send_buff[300]; + +/******************************************************************************* +* Function Name : XX +* Description : ��ʼ��һЩ���ò��� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void ConfigData_Init(void) +{ + uint16_t Temp = 0; + uint8_t length = 0; + uint8_t i; + uint8_t DotCounter = 0; //�ָ��������� + struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ + + printf("\r\nConfigData_Init start...\r\n"); //����ʹ�� + + Module_Flash_Read(CollectedDateCount_ADDR, &(businessData.DataCount), 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (businessData.DataCount > 15) //�豸�����ⲿ��λ + { + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + } + printf("\n\r businessData.DataCount:%d \r\n", businessData.DataCount); + + Module_Flash_Read(CollectedDate_ADDR, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + Module_Flash_Read(HistoryDateCount_ADDR, &(businessData.HistoryDateCount), 1); //��Flash�ж�ȡ��ʷ���ݳ��� + Module_Flash_Read(SendCount_ADDR, &(businessData.SendCount), 1); //��Flash�ж�ȡδ���͵Ĵ��� + Module_Flash_Read(ParameterReply_ADDR, &(businessData.ParameterReply), 1); //��Flash�ж�ȡ�������ûظ� + printf("\r\n-SendCount:-%d---.\r\n", businessData.SendCount); //����ʹ�� + printf("\r\n-ParameterReply:-%d---.\r\n", businessData.ParameterReply); //����ʹ�� + printf("\r\n-HistoryDateCount:-%d---.\r\n", businessData.HistoryDateCount); //����ʹ�� + + if(businessData.HistoryDateCount > 250) + { + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + } + else if(businessData.HistoryDateCount>0) + { + Module_Flash_Read(HistoryDate_ADDR, history_send_buff, businessData.HistoryDateCount); //��Flash�ж�ȡ��ʷ���� + } + for(i = 0; i < businessData.DataCount; i++) + { + printf("%.2x ", businessData.CollectData[i][0]); + printf("%.2x ", businessData.CollectData[i][1]); + printf("%.2x ", businessData.CollectData[i][2]); + printf("%.2x ", businessData.CollectData[i][3]); + } + + Module_Flash_Read(StartTime_ADDR, businessData.StartTime, 5); //��Flash�ж�ȡ�ɼ�ʱ�� + printf("**************��һ�βɼ�ʱ��:%d-%d-%d %d:%d***********\r\n", businessData.StartTime[0] + 2000, businessData.StartTime[1], //����ʹ�� + businessData.StartTime[2], businessData.StartTime[3], businessData.StartTime[4]); //����ʹ�� + +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectPeriod_ADDR, ConfigData.CollectPeriod_Byte, 2); //��Flash�ж�ȡ�ɼ���� + Temp = ConfigData.CollectPeriod_Byte[1] * 256 + ConfigData.CollectPeriod_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 96)) + { + DeviceConfig.CollectPeriod = Temp; + } + else + { + DeviceConfig.CollectPeriod = 1; //Ĭ�����ã�ÿ��60���Ӳɼ�һ�����ݡ� + } + printf("\r\n-CollectPeriod:-%2d---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� +////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectNum_ADDR, ConfigData.CollectNum_Byte, 2); //��Flash�ж�ȡ�ɼ����� + Temp = ConfigData.CollectNum_Byte[1] * 256 + ConfigData.CollectNum_Byte[0]; //�͵�ַ��Ӧ���ֽ� +// Temp = 1; + if((Temp > 0) && (Temp <= 24)) //ÿ�������ϱ����ɼ�15������ + { + DeviceConfig.CollectNum = Temp; + } + else + { + DeviceConfig.CollectNum = 12; //Ĭ�����ã�ÿ�������ϱ��ɼ�12�����ݡ� + } + printf("\r\n-CollectNum:-%2d---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(UploadCycle_ADDR, ConfigData.UploadCycle_Byte, 2); //��Flash�ж�ȡ�ϱ����� + Temp = ConfigData.UploadCycle_Byte[1] * 256 + ConfigData.UploadCycle_Byte[0]; + if((Temp > 0) && (Temp <= 1440)) + { + DeviceConfig.UploadCycle = Temp; + } + else + { + DeviceConfig.UploadCycle = 1; //Ĭ�����ã������ϱ�����Ϊ24Сʱ����ÿ���ϴ�1�����ݡ� + } + printf("\r\n-UploadCycle:-%2d---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(CollectTime_ADDR, ConfigData.CollectTime_Byte, 2); //��Flash�ж�ȡ�ɼ�ʱ�� + Temp = ConfigData.CollectTime_Byte[1] * 256 + ConfigData.CollectTime_Byte[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp <= 1440)) //���Ʋɼ�ʱ�䷶ΧΪ0-1440���� + { + DeviceConfig.CollectTime = Temp; + } + else + { + DeviceConfig.CollectTime = 60; //Ĭ�����ã��賿1�㿪ʼ������ + } + printf("\r\n-CollectTime:-%2d---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(MountHeight_ADDR, ConfigData.MountHeight.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.MountHeight.Data_Float >= 0) && (ConfigData.MountHeight.Data_Float <= 100)) //���Ƶ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.MountHeight.Data_Float = ConfigData.MountHeight.Data_Float; + } + else + { + DeviceConfig.MountHeight.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float ); //����ʹ�� +//////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(AlarmValue_ADDR, ConfigData.AlarmValue.Data_Hex, 4); //��Flash�ж�ȡ��Ũ�ȱ�����ֵ + if((ConfigData.AlarmValue.Data_Float >= 0) && (ConfigData.AlarmValue.Data_Float <= 100)) //���Ƹ�Ũ�ȱ�����ֵΪ0-100 + { + DeviceConfig.AlarmValue.Data_Float = ConfigData.AlarmValue.Data_Float; + } + else + { + DeviceConfig.AlarmValue.Data_Float = 0; //Ĭ�����ã�0�� + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float ); //����ʹ�� +///////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(RetryNum_ADDR, &ConfigData.RetryNumSet, 1); //��Flash�ж�ȡ�ɼ��ش����� + Temp = ConfigData.RetryNumSet; + if((Temp > 0) && (Temp <= 10)) //�ش���������Ϊ10 + { + DeviceConfig.RetryNum = Temp; + } + else + { + DeviceConfig.RetryNum = 3; //Ĭ�����ã����ݶ�ʧ���߳���ʱ�������3�Ρ� + } + printf("\r\n-RetryNum:-%d---.\r\n", DeviceConfig.RetryNum ); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerIP_ADDR, (uint8_t *)ConfigData.SMS_Set_ServerIP, 15); //��Flash�ж�ȡIP��ַ + for(Temp = 0; Temp < 15;) + { + if(((ConfigData.SMS_Set_ServerIP[Temp] >= '0') && (ConfigData.SMS_Set_ServerIP[Temp] <= '9')) || (ConfigData.SMS_Set_ServerIP[Temp] == '.')) + { + Temp++; + if(ConfigData.SMS_Set_ServerIP[Temp] == '.')DotCounter++; + } + else + { + break; + } + } + length = Temp; + if((length >= 7) && (DotCounter == 3)) //��IP��ַ�Ϸ���������ɸѡ�������д����� + { + memcpy(DeviceConfig.ServerIP, ConfigData.SMS_Set_ServerIP, length); + } + else + { + memcpy(DeviceConfig.ServerIP, IPNET_MAIN, strlen(IPNET_MAIN)); //��ȡ������Чʱ����ʼ��������IP + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*)IPNET_MAIN, strlen(IPNET_MAIN)); + } + printf("\n\r<**********Data Upload Server IP: %s ********>\r\n", DeviceConfig.ServerIP); //����ʹ�� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Module_Flash_Read(ServerPort_ADDR, ConfigData.SMS_Set_ServerPort, 2); //��Flash�ж�ȡ�������˿ں� + Temp = ConfigData.SMS_Set_ServerPort[1] * 256 + ConfigData.SMS_Set_ServerPort[0]; //�͵�ַ��Ӧ���ֽ� + if((Temp > 0) && (Temp < 65535)) //���Ʋɼ�ʱ�䷶ΧΪ0-65535���� + { + DeviceConfig.ServerPort = Temp; + } + else + { + DeviceConfig.ServerPort = PORTNUM_MAIN; //��ȡ������Чʱ����ʼ���������˿ں� + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + printf("\n\r<**********Data Upload Server Port: %d **************>\r\n", DeviceConfig.ServerPort); //����ʹ�� + + if(DeviceConfig.CollectNum > 24) + { + DeviceConfig.CollectNum = 12; //�������ݷ����Լ����ݴ洢�ռ�����ƣ����ݲɼ���������������5���� + } + DeviceConfig.BatteryCapacity = 0x64; //��ص������ݶ�Ϊ100% +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Level_DataCollect(void) +{ + uint8_t i = 0; //����ʹ�� + union Hfloat levelData; + + i = businessData.DataCount; + levelData.Data_Float = Gas_DataGet(); + if(levelData.Data_Float <= 100) + { + businessData.Level_Float = levelData.Data_Float; + + for(uint16_t j = 0; j < 4; j++) + { + businessData.Level_Date[i].Data_Hex[j] = levelData.Data_Hex[j]; + businessData.CollectData[i][j] = levelData.Data_Hex[j]; + } + + if(i == 0) + { + businessData.CollectTime[i] = (DeviceConfig.Time_Hour) * 60 + (DeviceConfig.Time_Min); //��һ�����ݲɼ�ʱ�� + businessData.StartTime[0] = DeviceConfig.Time_Year; + businessData.StartTime[1] = DeviceConfig.Time_Mon; + businessData.StartTime[2] = DeviceConfig.Time_Mday; + businessData.StartTime[3] = DeviceConfig.Time_Hour; + businessData.StartTime[4] = DeviceConfig.Time_Min; + + Module_Flash_Write(StartTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (businessData.StartTime), 5); + } + else + { + businessData.CollectTime[i] = (businessData.CollectTime[i - 1]) + DeviceConfig.CollectPeriod; //����ÿ�����ݲɼ�ʱ��Ϊ��ǰһ�����ݻ��������Ӳɼ���� + } + businessData.DataCount = (businessData.DataCount) + 1; + } +} + +/******************************************************************************* +* Function Name : BusinessData_Collection +* Description : ҵ�����ݲɼ� +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Collection(void) +{ + if(businessData.DataCount < DeviceConfig.CollectNum) + { + Level_DataCollect(); + } +} + +/******************************************************************************* +* Function Name : BusinessData_Storage +* Description : ҵ�����ݴ洢 +* Input : none +* Output : none +* Return : none +*******************************************************************************/ +void BusinessData_Storage(void) +{ + if(businessData.DataCount <= DeviceConfig.CollectNum) + { + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + Module_Flash_Write(CollectedDate_ADDR - FLASH_TEST_BASE, (uint8_t *)&(businessData.CollectData), businessData.DataCount * 4); //��Flash�ж�ȡ�ɼ�Һλ���� + } +} \ No newline at end of file diff --git a/API.h b/API.h new file mode 100644 index 0000000..5f3aeb8 --- /dev/null +++ b/API.h @@ -0,0 +1,24 @@ +#ifndef _API_H_ +#define _API_H_ + +#include +#include +#include +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "rtc.h" +#include "gprs.h" +#include "NB-IOT.h" +#include "bsp_ds2782.h" +#include "TEA.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" + +void ConfigData_Init(void); +void Level_DataCollect(void); +double Gas_DataGet(void); +void BusinessData_Collection(void); +void BusinessData_Storage(void); + +#endif diff --git a/AiderProtocol.c b/AiderProtocol.c new file mode 100644 index 0000000..939fffb --- /dev/null +++ b/AiderProtocol.c @@ -0,0 +1,506 @@ +#include "gprs.h" +#include "string.h" +#include "AiderProtocol.h" +#include "API-Platform.h" +#include "rtc.h" +#include "NB-IOT.h" +#include "TEA.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "bsp_ds2782.h" +#include "flash.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* +1����������֡�д��ڻس����з������ͨ��3Gģ��AT�����ϴ����ݵij����д�ȷ�ϡ� +2������һ֡�ϴ����ݵ����ݲɼ���ʽ�����ַ������ֱ��ǣ� +һ�����ݲɼ�ʱ�������������ϴ�ʱ��֮��ƽ�����䣬��ʱ��Ҫ�ɼ�һ�����ݽ������ߣ�ͬʱ���ɼ��������ݴ����ⲿ�洢���� +���������߱��ݼĴ�����¼���ݲɼ������� +�������ݲɼ����ԶС�������ϴ�ʱ��������ʱ���ѣ�����֮�������ɼ����ݣ�N�����ݲɼ����֮�������ϴ����������� +�ϴ���ɣ������������ش�����ٴν������ģʽ�� +ĿǰĬ�ϲ��÷����� +*/ + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +extern struct DeviceSet DeviceConfig; //������Ϣ�ṹ�� +extern struct BusinessData businessData; +extern struct DataFrame dataFrame; + +extern uint8_t DeviceID[6]; +extern uint16_t NodeAddr; +extern uint8_t RouteControl; + +extern char IMEI_Code[32]; //��Ч15λ����λ0 +extern char ICCID_Code[32]; //��Ч20λ����λ0 +extern uint8_t SIMCard_Type; +extern uint8_t signalValue[6]; +extern uint8_t Flash_Write_Flag[9]; +extern double GASData; +extern uint16_t ACR; + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame StartupSend; + struct SpecialDataFrame* pDataFrame = &StartupSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i, g = 0, NB_ReTry=0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + + printf("\r\n**********StartupRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + StartupSend.Preamble = 0xA3; + StartupSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + StartupSend.DeviceID[g] = DeviceID[g]; + } + StartupSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + StartupSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 12 : + StartupSend.PDU_Type = (12 << 8) + (1 << 7) + 0x1C; //SetResponse + break; + case 8 : + StartupSend.PDU_Type = (8 << 8) + (1 << 7) + 0x1C; //StartupRequest + break; + default: + StartupSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + StartupSend.PDU_Type = ntohs(StartupSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + StartupSend.Seq = 0x01; + LengthKey = CoreLength; + + if(Optype == 8) + { + StartupSend.TagList[0].OID_Command = ntohl(CARRIER_CODE); //�豸������� + StartupSend.TagList[0].Width = 35; + memcpy(StartupSend.TagList[0].Value,IMEI_Code + 7,15); + memcpy(StartupSend.TagList[0].Value+15,ICCID_Code,20); + printf("CARRIER_CODE:%s",StartupSend.TagList[0].Value); + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + } + else if(Optype == 12) + { + StartupSend.TagList[0].OID_Command = ntohl(DEF_NR); //�ش����� + StartupSend.TagList[0].Width = 1; + StartupSend.TagList[0].Value[0] = DeviceConfig.RetryNum; + StartupSend.Length = LengthKey + StartupSend.TagList[0].Width + 6; + + StartupSend.TagList[1].OID_Command = ntohl(CLT1_ITRL1); + StartupSend.TagList[1].Width = 2; + StartupSend.TagList[1].Value[0] = DeviceConfig.CollectPeriod >> 8; //���ݲɼ���������ֽ���ǰ + StartupSend.TagList[1].Value[1] = DeviceConfig.CollectPeriod & 0xff; //���ݲɼ���������ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + + StartupSend.TagList[2].OID_Command = ntohl(UPLOAD_CYCLE); + StartupSend.TagList[2].Width = 2; + StartupSend.TagList[2].Value[0] = DeviceConfig.UploadCycle >> 8; //�����ϱ����ڣ����ֽ���ǰ + StartupSend.TagList[2].Value[1] = DeviceConfig.UploadCycle & 0xff; //�����ϱ����ڣ����ֽ��ں� + StartupSend.Length = StartupSend.Length + StartupSend.TagList[1].Width + 6; + } + + + StartupSend.Length = ntohs(StartupSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &StartupSend.Preamble, 1); + memcpy(pSendBuff+1, &StartupSend.Version, 1); + memcpy(pSendBuff+2, &StartupSend.Length, 2); + memcpy(pSendBuff+4, &StartupSend.DeviceID, 6); + memcpy(pSendBuff+10, &StartupSend.RouteFlag, 1); + memcpy(pSendBuff+11, &StartupSend.NodeAddr, 2); + memcpy(pSendBuff+13, &StartupSend.PDU_Type, 2); + memcpy(pSendBuff+15, &StartupSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + if(Optype == 8) + { + pChar = (char*) & (StartupSend.TagList[0].OID_Command); + ValidLength = StartupSend.TagList[0].Width + 6; //����1������Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[0].Width = ntohs(StartupSend.TagList[0].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + else if(Optype == 12) + { + for(i = 0; i < 3; i++) + { + pChar = (char*) & (StartupSend.TagList[i].OID_Command); //Tag + ValidLength = StartupSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + StartupSend.TagList[i].Width = ntohs(StartupSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + printf("\r\n*********pSendBuff!***********\r\n"); + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset ); // Update the CRC value + StartupSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + + for(NB_ReTry=0; NB_ReTry<1; NB_ReTry++) + { + printf("\r\n*********NB Start!***********\r\n"); + printf("\r\n***************���ʹ���:%d***************\r\n",g); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x099C) + { + printf("\r\nReceive StartupResponse success!\r\n"); //����ʹ�� + break; + } + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + return Send_Flag; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t TrapRequest(uint8_t* pSendBuff, uint8_t Optype) +{ + struct SpecialDataFrame TrapSend; + struct SpecialDataFrame* pDataFrame = &TrapSend; + char* pChar = NULL; + uint16_t CrcData = 0; + uint8_t i,g = 0; + uint8_t Offset = 0; //���ͻ����ַƫ�Ʊ��� + uint8_t ValidLength = 0; //��Ч���ݳ��ȣ�ָ1��Tag�ռ���ʵ�ʴ洢��Ч���ݵij��ȡ� + uint8_t LengthKey = 0; //���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ + const uint8_t PreLength = 16; //֡ǰ׺���ȣ�ָ��֡ǰ���뵽OID���У�����Tag���У������ݳ��ȣ�����֡ǰ���룬��������OID���У�����Tag���У� + const uint8_t CoreLength = 12; //�ؼ���Ϣ���ȣ�ָ���ݾ��ɲ��ֳ����ֶ�ָʾ����ֵ��ȥ��Tag���к�ʣ�����ݵij��� + uint16_t Send_Flag = 0; + uint8_t TEA_Data[MAX_SEND_DATA]; //�����Ժ����ݳ���Ϊ8�ı���������ǰ���ȳ���8����ȡ�������8 + uint16_t TEA_Data_Len; + uint8_t count = 0; + + printf("\r\n**********TrapRequest**********\r\n"); + + pChar = (char*)pDataFrame; + memset(pChar, 0x00, sizeof(struct SpecialDataFrame)); //��ʼ���ṹ�� + TrapSend.Preamble = 0xA3; + TrapSend.Version = 0x20; + for(g = 0; g < 6; g++) + { + TrapSend.DeviceID[g] = DeviceID[g]; + } + TrapSend.RouteFlag = RouteControl; //ͨѶ��ʽ + + TrapSend.NodeAddr = ntohs(NodeAddr); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + + switch (Optype) + { + case 2 : + TrapSend.PDU_Type = (2 << 8) + (1 << 7) + 0x1C; //GetResponse + break; + default: + TrapSend.PDU_Type = (4 << 8) + (1 << 7) + 0x1C; //Ĭ��ΪTrapRequest + printf("\r\nOptype:%d\r\n", Optype); //����ʹ�ã� �豸������� + break; + } + TrapSend.PDU_Type = ntohs(TrapSend.PDU_Type); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.Seq = 0x01; + LengthKey = CoreLength; + + if(ACR > 17000) + ACR = 17000; + DeviceConfig.BatteryCapacity = ACR/170; //��ص��� + TrapSend.BattEnergy.OID_Command = ntohl(DEVICE_QTY); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.BattEnergy.Width = 1; // + TrapSend.BattEnergy.Value[0] = DeviceConfig.BatteryCapacity; //�����ϴ�ʱ�ĵ��ʣ����� + LengthKey = LengthKey + 6 + TrapSend.BattEnergy.Width; + + TrapSend.SysTime.OID_Command = ntohl(SYSTERM_DATA); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.SysTime.Width = 3; // + TrapSend.SysTime.Value[0] = businessData.StartTime[0]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[1] = businessData.StartTime[1]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + TrapSend.SysTime.Value[2] = businessData.StartTime[2]; //��Ϣ�������ڣ��ǵ�ǰ���ڣ��� + LengthKey = LengthKey + 6 + TrapSend.SysTime.Width; + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + printf("\r\n--------Module_ME3616_menginfo_Check-----------\r\n"); + Module_ME3616_menginfo_Check(); + + TrapSend.TagList[0].OID_Command= ntohl(NB_PCI); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[0].Width =2; + TrapSend.TagList[0].Value[0] = signalValue[0]; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + TrapSend.TagList[0].Value[1] = signalValue[1]; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[0].Width; + + TrapSend.TagList[1].OID_Command= ntohl(NB_RSRP); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[1].Width =2; + TrapSend.TagList[1].Value[0] = signalValue[2]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.TagList[1].Value[1] = signalValue[3]; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[1].Width; + + TrapSend.TagList[2].OID_Command= ntohl(NB_SNR); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[2].Width =2; + TrapSend.TagList[2].Value[0] = signalValue[4]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.TagList[2].Value[1] = signalValue[5]; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + TrapSend.TagList[3].OID_Command= ntohl(SENSOR_STATE); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + TrapSend.TagList[3].Width =1; + if(GASData > 245) + { + TrapSend.TagList[3].Value[0] = 1; //ȼ���������ɼ�ʧ�� + }else if(GASData > 235) + { + TrapSend.TagList[3].Value[0] = 2; //ȼ�������������쳣 + }else + { + TrapSend.TagList[3].Value[0] = 0; //ȼ���������������� + } + TrapSend.Tag_Count++; + LengthKey = LengthKey + 6 + TrapSend.TagList[2].Width; + + for(i = 4, TrapSend.Tag_Count = 4, TrapSend.Length = LengthKey ; i < businessData.DataCount+4; i++) //��ÿһ������Tag��ֵ + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + TrapSend.TagList[i].OID_Command = (DeviceConfig.CollectPeriod << 11) + (businessData.StartTime[3] * 60 + businessData.StartTime[4]) + (0xC5 << 24); + printf("\r\n----TrapSend.TagList[i].OID_Data1:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ�� + TrapSend.TagList[i].OID_Command = ntohl(TrapSend.TagList[i].OID_Command); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ���? + printf("\r\n----TrapSend.TagList[i].OID_Command:%lx----\r\n", TrapSend.TagList[i].OID_Command); //����ʹ��? + TrapSend.TagList[i].Width = 4 * businessData.DataCount; + TrapSend.Length = TrapSend.Length + 10; + } + else + { + TrapSend.Length = TrapSend.Length + 4; + } + + TrapSend.TagList[i].LevelData.Value[0] = businessData.CollectData[i-4][0]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[1] = businessData.CollectData[i-4][1]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[2] = businessData.CollectData[i-4][2]; //�ɼ��������� + TrapSend.TagList[i].LevelData.Value[3] = businessData.CollectData[i-4][3]; //�ɼ��������� + TrapSend.Tag_Count++ ; //������Tag���м������������ɼ�����Tag + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + TrapSend.Length = ntohs(TrapSend.Length); //���㳤���ֶ� + + memcpy(pSendBuff, &TrapSend.Preamble, 1); + memcpy(pSendBuff+1, &TrapSend.Version, 1); + memcpy(pSendBuff+2, &TrapSend.Length, 2); + memcpy(pSendBuff+4, &TrapSend.DeviceID, 6); + memcpy(pSendBuff+10, &TrapSend.RouteFlag, 1); + memcpy(pSendBuff+11, &TrapSend.NodeAddr, 2); + memcpy(pSendBuff+13, &TrapSend.PDU_Type, 2); + memcpy(pSendBuff+15, &TrapSend.Seq, 1); + +// memcpy(pSendBuff, pChar, PreLength); //����Tag֮ǰ�����ݵ�����Buff + Offset = PreLength; //ָ��ƫ�Ƶ�ַ + + pChar = (char*) & (TrapSend.BattEnergy.OID_Command); //��ص���Tag + ValidLength = TrapSend.BattEnergy.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.BattEnergy.Width = ntohs(TrapSend.BattEnergy.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //���Ƶ�ص���Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + pChar = (char*) & (TrapSend.SysTime.OID_Command); //ϵͳ����Tag + ValidLength = TrapSend.SysTime.Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.SysTime.Width = ntohs(TrapSend.SysTime.Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ϵͳʱ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + + for(i = 0; i < 4; i++) + { + pChar = (char*) & (TrapSend.TagList[i].OID_Command); //�ź�ǿ��Tag + ValidLength = TrapSend.TagList[i].Width + 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[i].Width = ntohs(TrapSend.TagList[i].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //�����ź�ǿ��Tag���ݵ�����Buff + Offset = Offset + ValidLength; + } + + for(i = 4; i < TrapSend.Tag_Count; i++) // + { + if(i == 4) //��һ������TAG����OID��LENGTH + { + pChar = (char*) & (TrapSend.TagList[4].OID_Command); + ValidLength = 6; //����1��Tagʵ��ռ�õ��ֽڿռ� + TrapSend.TagList[4].Width = ntohs(TrapSend.TagList[4].Width); //����Ϊ�����򣬼����ֽ���ǰ�����ֽ��ں� + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + pChar = (char*) & (TrapSend.TagList[i].LevelData); + ValidLength = 4; //����1��Tagʵ��ռ�õ��ֽڿռ� + + memcpy((pSendBuff + Offset), pChar, ValidLength); //����ÿһ������Tag������Buff + Offset = Offset + ValidLength; + } + + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + //payload ���� + memcpy(TEA_Data, (char*)(pSendBuff + PreLength), (Offset - PreLength)); + TEA_Data_Len = Tea_Encrypt(TEA_Data, (Offset - PreLength)); + + memcpy((pSendBuff + PreLength), TEA_Data, TEA_Data_Len); //���Ƽ���֮���payload���ݵ�����Buff + Offset = PreLength + TEA_Data_Len; + + CrcData = CRC16(pSendBuff, Offset); // Update the CRC value + TrapSend.CrcCode = CrcData; + + pSendBuff[Offset++] = CrcData >> 8; //CRC���ֽ���ǰ + pSendBuff[Offset++] = CrcData & 0xff; //CRC���ֽ��ں� + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + printf("\r\n*********NB Start!***********\r\n"); + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + Send_Flag =SendMessage_NB_T(pSendBuff, Offset); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + Send_Flag =SendMessage_NB_MU(pSendBuff, Offset); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + + if(Send_Flag == 0x039C) + { + printf("\r\nReceive SetRequest success!\r\n"); //����ʹ�� + if(Flash_Write_Flag[0] == 1) + { + Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + if(Flash_Write_Flag[1] == 1) + { + Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + if(Flash_Write_Flag[2] == 1) + { + Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + if(Flash_Write_Flag[3] == 1) + { + Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + if(Flash_Write_Flag[4] == 1) + { + Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + if(Flash_Write_Flag[5] == 1) + { + Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + if(Flash_Write_Flag[6] == 1) + { + Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + if(Flash_Write_Flag[7] == 1) + { + Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), sizeof(DeviceConfig.ServerIP)); + } + if(Flash_Write_Flag[8] == 1) + { + Module_Flash_Write(ServerPort_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerPort), 2); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + if(dataFrame.Tag_Count > 1) + { + businessData.ParameterReply = 1; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + } + } + else + { + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &Offset, 1); + Module_Flash_Write(HistoryDate_ADDR - FLASH_TEST_BASE, pSendBuff, Offset); + businessData.SendCount = 1; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + } + + printf("\r\n----Length:%d----\r\n", Offset); //����ʹ�� + for(g = 0; g < Offset; g++) + { + printf("%.2x ", pSendBuff[g]); //����ʹ�� + } + + return Send_Flag; +} \ No newline at end of file diff --git a/AiderProtocol.h b/AiderProtocol.h new file mode 100644 index 0000000..1764724 --- /dev/null +++ b/AiderProtocol.h @@ -0,0 +1,302 @@ +#ifndef _AIDERPROTOCOL_H_ +#define _AIDERPROTOCOL_H_ + +#include "API-Platform.h" +#include "stdio.h" +#define MAX 10 //����һ֡������������OID���� +#define MAX_SEND_DATA 152 //����һ�������ϱ�����ϱ��ֽ��� + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma push + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pack( 1 ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union Hfloat +{ + uint8_t Data_Hex[4]; + float Data_Float; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt16 +{ + uint8_t Data_Hex[2]; + uint16_t Data_Int16; +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +union HInt32 +{ + uint8_t Data_Hex[4]; + uint32_t Data_Int32; +}; //���������ݸ�ʽת�� +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceStatus +{ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡� + uint8_t CorrRateSensorStatus; //��ʴ���ʴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t TemperatureSensorStatus; //�¶ȴ�����״ָ̬ʾ������0��ʾ����������1��ʾ���ݲɼ�ʧ�ܣ�2��ʾ�����쳣��3��ʾ̽ͷδ���� +// uint8_t AlarmFlag; //����ָʾ��ǰҺλ�Ƿ�Ԥ��ֵ,0��ʾ������1��ʾδ������2��ʾ��λ����ѯ���� + +}; +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SenserData +{ + + uint16_t CollectTime; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� +// float ResistanceRatio; //�ɼ����ĵ����ֵ��̽�����Ȳο����裩 +// float ReferenceResistance; //�ɼ����IJο�������ֵ +// float CorrRateData; //�ɼ����ĸ�ʴ�������� + float ProbeLossData; //�ɼ�����̽��������� + +// uint8_t DataCount; //�ɼ�������������,�¶�ѹ�����ݳɶԳ��� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct BusinessData +{ + + uint16_t CollectTime[24]; //ÿ�����ݶ�Ӧ�IJɼ�ʱ�䣬��ÿ�����Ϊ�ο�����λ���� + uint8_t CollectData[24][4]; //�ɼ��������� + uint8_t StartTime[5]; //�ɼ���һ�����ݵ�ʱ������ + uint8_t DataCount; //�ɼ������������� + uint8_t SendCount; //δ���͵Ĵ��� + uint8_t ParameterReply; //�������ûظ� + uint8_t HistoryDateCount; //δ���͵���ʷ�������� + float Level_Float; + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } Level_Date[24]; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION:����Flash���ݴ洢�ͽ��� +// ----------------------------------------------------------------------------- +struct Config_RegPara +{ + uint8_t CollectTime_Byte[2]; //���ݲɼ�ʱ�䣬ʹ������洢���Է���Flash��д + uint8_t CollectPeriod_Byte[2]; //���ݲɼ������ʹ������洢���Է���Flash��д + uint8_t CollectNum_Byte[2]; //�ɼ�������ʹ������洢���Է���Flash��д + uint8_t UploadCycle_Byte[2]; //�����ϴ����ڣ�ʹ������洢���Է���Flash��д + uint8_t RetryNumSet; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + char CurrentPhoneNum[16]; //��ǰͨ���ֻ����룬�ַ�����ʽ + char SMS_Set_ServerIP[16]; //�������÷�����IP���ַ�����ʽ + uint8_t SMS_Set_ServerPort[2]; //�������÷������˿ںţ��ַ�����ʽ + uint8_t SignalIntensity; //�ź�ǿ�� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DeviceSet //���޸� +{ + uint16_t CollectTime; //���ݲɼ�ʱ�䣬���豸��ʼ������ʱ�䣬��λ���� + uint16_t CollectPeriod; //���ݲɼ�������������������ݲɼ���ʱ��������λ���� + uint16_t CollectNum; //�ɼ���������һ���ϴ����������� + uint16_t UploadCycle; //�����ϱ����ڣ����豸���������ڣ���λ���� + uint8_t RetryNum; //�����ش����� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } MountHeight; //Һλ̽ͷ�����׵ĸ߶� + union + { + uint8_t Data_Hex[4]; + float Data_Float; + } AlarmValue; //������ֵ + uint8_t BatteryCapacity; //���������⣬����xx%����Ϊ�ٷֺ�ǰ��IJ��֡�//��Ȼ�������ò���������Ϊ��������֯���㣬�Է������ò����ṹ���� + uint8_t Time_Sec; //ϵͳʱ�䣬�� + uint8_t Time_Min; //ϵͳʱ�䣬�� + uint8_t Time_Hour; //ϵͳʱ�䣬Сʱ + uint8_t Time_Mday; //ϵͳ���ڣ��� + uint8_t Time_Mon; //ϵͳ���ڣ��� + uint8_t Time_Year; //ϵͳ���ڣ��� + uint8_t NetId; //433ģ������ID��1-255 + uint8_t PDUType; //PDUType��1-255 + uint8_t RouteFlag; //RouteFlag��1-255 + uint8_t DeviceType; //�豸���ͣ�1-255 + char ServerIP[16]; //������IP + uint16_t ServerPort; //�������˿� +}; + +typedef enum +{ + DEF_NR = 0x1000000A, //�ش����� + SYSTERM_DATA = 0x10000050, //ϵͳ���� + SYSTERM_TIME = 0x10000051, //ϵͳʱ�� + CLT1_STIME1 = 0x10000104, //һʱ���ɼ�ʱ�� + CLT1_ITRL1 = 0x10000105, //һʱ���ɼ���� + CLT1_CNT1 = 0x10000106, //һʱ���ɼ����� + UPLOAD_CYCLE = 0x10000062, //�����ϱ����� + MOUNT_HEIGHT = 0x10000060, //Һλ̽ͷ�����׵ĸ߶� + ALARM_VALUE = 0x10000901, //������ֵ + DEVICE_STATE = 0x60000100, //�豸״ָ̬ʾ���ϵ磩 + SENSOR_STATE = 0x60000009, //������״ָ̬ʾ���ϵ磩 + DEVICE_QTY = 0x60000020, //��ص��� + DEVICE_WAKEUP= 0x60000200, //�豸״ָ̬ʾ�����ѣ� + FRAM_STATE = 0x60000300, //���ݽ���״ָ̬ʾ + DATA_REQUEST = 0x20000001, //��������ѯ���� + NET_ID = 0x10001001, //433ģ������ID + CARRIER_CODE = 0x10000063, //IMEI&ICCID + NB_PCI = 0x60000511, //NB��С������С��PCI�� + NB_RSRP = 0x60000513, //NB��С��RSRPֵ + NB_SNR = 0x60000516, //NB��С��lart SNRֵ + BSS_IP = 0x10000022, //�豸IP��ַ����Ҫ����3G���豸ͨ���������� + BSS_PORT = 0x10000023 //�豸�˿ںţ���Ҫ����3G���豸ͨ���������� +} CommandType; //ͨ�Ź�����ʹ�õ���OID���ϣ�����ҵ���ϱ�OID�� + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct TagStruct //���ڴ洢������������ +{ + CommandType OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[32]; //����ֵ����WidthΪ0ʱ��Value����Բ����ڣ���ǰӦ������Value�����ռ��32�ֽ� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialTagStruct //���ڴ洢ҵ���ϱ����� +{ + uint32_t OID_Command; //����OID���� + uint16_t Width; //Value��ij��� + uint8_t Value[40]; + union //���������壬���ں������ض��ֽڲ��� + { + uint8_t Value[4]; //ieee754��ʽ��ʾ��ҵ������ + float Data_F; //��������ʽ��ʾ��ҵ������ + } LevelData; +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct CommandFrame //��ѯ��������֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + CommandType OID_List[MAX]; //OID���У�һ֡���ݿ��԰������OID����಻����20�� + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t OID_Count; //OID���м����� +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct DataFrame //�·����ò�����֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰������Tag����಻����20��,����һ֡�����ܳ��ȣ�����9�ֽ�֡ͷ�����ܳ���256 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� + +}; + +//�ṹ������ +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +struct SpecialDataFrame //ҵ�������ϴ�֡��ʽ +{ + uint8_t Preamble; //֡ǰ���� + uint8_t Version; //Э��汾�� + uint16_t Length; //��Ч���ݳ��ȣ�����Length�ֶε�CRC�ֶΰ�����ʵ�����ݳ��ȣ���λΪByte��������Length�ֶκ�CRC�ֶΣ�OID_List������ʵ��ʹ�õ����ݿռ�Ϊ׼�� + uint8_t DeviceID[6]; //�豸ID�� + uint8_t RouteFlag; //·�ɱ�־ + uint16_t NodeAddr; //·�ɽڵ��ַ + uint16_t PDU_Type; //����ָʾ + uint8_t Seq; //������ţ�1~255�� + struct TagStruct SysTime; //ϵͳʱ�䣬��ʾ��һ�����ݵIJɼ�ʱ�� + struct TagStruct BattEnergy; //���ʣ�����������xx%����Ϊ�ٷֺ�ǰ��IJ��� + struct SpecialTagStruct TagList[MAX]; //Tag���У�һ֡���ݿ��԰���1��Tag,����һ֡�����ܳ��Ȳ��ܳ���100 + uint16_t CrcCode; //16λCRCУ���룬���㷶ΧΪ��Preamble��CrcCode��ȫ�����ݣ�����Preamble��CrcCode��CrcCode��ֵΪ0xFFFF�� + uint8_t Tag_Count; //Tag���м����� +// uint8_t nb_rssi; +}; +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#if defined ( __CC_ARM ) + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#pragma pop + +// ----------------------------------------------------------------------------- +// DESCRIPTION: +// ----------------------------------------------------------------------------- +#endif + + + + +uint16_t TrapRequest(uint8_t* pSendBuff,uint8_t Optype); +uint16_t StartupRequest(uint8_t* pSendBuff, uint8_t Optype); +#endif + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3eeabcc --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +################################################################################ +## +## File : Makefile +## +## Copyright (C) 2013-2018 ZTEWelink. All rights reserved. +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +## Author : lixingyuan@gosuncn.cn +## +## $Date: 2018/02/08 08:45:36GMT+08:00 $ +## +################################################################################ + +################################################################################ +# sdk path +################################################################################ +SDK_PATH := $(realpath ../..) + +############################################################################### +# target +################################################################################ +TARGET := user + +################################################################################ +# common variables +################################################################################ +DEBUG := 1 + +################################################################################ +# source +################################################################################ +C_SOURCES := $(wildcard *.c) \ + $(SDK_PATH)/src/gsdk_ril_cmds.c \ + $(SDK_PATH)/FreeRTOS/Source/portable/MemMang/heap_1.c + +S_SOURCES := + +################################################################################ +# CFLAGS +################################################################################ +C_DEFS := + +C_INCLUDES := + +################################################################################ +# ASFLAGS +################################################################################ +# macros for gcc +AS_DEFS := + +# includes for gcc +AS_INCLUDES := + +################################################################################ +# LDFLAGS +################################################################################ +# libraries +LD_LIBS := -lgsdk + +################################################################################ +# Build rule +################################################################################ +include $(SDK_PATH)/scripts/rules.mk + +# *** EOF *** diff --git a/NB-IOT.c b/NB-IOT.c new file mode 100644 index 0000000..db9e07a --- /dev/null +++ b/NB-IOT.c @@ -0,0 +1,596 @@ +#include "string.h" +#include "NB-IOT.h" +#include "rtc.h" +#include "string.h" +#include "gprs.h" + +#include +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" +#include "gsdk_ril.h" +#include "lwip/sockets.h" +#include "lwip/ip.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +#define MAX_IP_BYTE 20 +#define MAX_HEX_SIZE 1024 +#define MAX_REGISTER_SIZE 1024 +#define LIFETIME 6000 +#define IMEI_SIZE 32 +#define IMSI_SIZE 32 +#define ICCID_SIZE 32 + +extern struct DeviceSet DeviceConfig; +extern struct Config_RegPara ConfigData;//������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +uint8_t handle_func_Flag; //handle_func����ע���־FLAG��ֻ��ע��һ�� + +uint8_t ESOC_Num=0; //�����׽��� +char IMEI_Code[32] = {0}; //��Ч15λ����λ0 +char ICCID_Code[32] = {0}; //��Ч20λ����λ0 + +uint8_t mtu_flag; +uint8_t SIMCard_Type; + +static char recv_buf[MAX_REGISTER_SIZE] = {0}; +static fd_set readfds; +static fd_set writefds; +static fd_set errorfds; +static int maxfd; +static int socket_id = -1; +static struct timeval tv; +static int error_code; + +uint16_t PTU_flag; + +uint8_t signalValue[6] = {0}; + +extern uint8_t PD2_Flag; + +/*information that come from iot appear will execute this function*/ +static int handle_func(const char *s, const char *extra, int len, void *data) +{ + (void)extra; + (void)len; + WARN_LOG("infomation come from iot is %s\r\n", s); + if (strlen(s) > (MAX_REGISTER_SIZE - 1)) + { + WARN_LOG("the information is too large ,please change the size of recv_buf\r\n"); + } + else + { + strncpy(data, s, MAX_REGISTER_SIZE); + } + PTU_flag = Receive_Data_Analysis((char *)s); + return GSDK_AT_UNSOLICITED_HANDLED; +} + +void str_to_hex(char *out, const char *in, int len) +{ + int i; + for (i = 0; i < len; i++) + { + sprintf(out + (i * 2), "%02x", in[i]); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Code_Read_NB(void) +{ + gsdk_status_t status = GSDK_ERROR; + char imsi_buf[IMSI_SIZE] = {0}; + + printf("\r\n*********NB Start!***********\r\n"); + + /*Get NB module IMEI number*/ + status = gsdk_ril_get_imei(IMEI_Code, IMEI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imei number cmd failed\r\n"); + } + + WARN_LOG("Get imei number success,imei:%s\r\n", IMEI_Code + 7); + +// vTaskDelay(500); + /*Get NB module ICCID number*/ + status = gsdk_ril_get_iccid(ICCID_Code, ICCID_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get iccid number cmd failed\r\n"); + } + WARN_LOG("Get iccid number success,iccid:%s\r\n", ICCID_Code); + +// vTaskDelay(500); + /*Get NB module IMSI number*/ + status = gsdk_ril_get_imsi(imsi_buf, IMSI_SIZE); + if (GSDK_ERROR == status) + { + DEBUG_LOG("Get imsi number cmd failed\r\n"); + } + WARN_LOG("Get imsi number success,imsi:%s\r\n", imsi_buf); + + mtu_flag = (*(imsi_buf + 3) - 48) * 10 + (*(imsi_buf + 4) - 48); + + if((mtu_flag == 11)||(mtu_flag == 3)||(mtu_flag == 5)) + { + printf("\r\n ===========����NB��===========!!\r\n"); + SIMCard_Type = 1; + } + else if((mtu_flag == 1) || (mtu_flag == 6) || (mtu_flag == 9) || (mtu_flag == 0) || (mtu_flag == 2) || (mtu_flag == 4) || (mtu_flag == 7) || (mtu_flag == 8) || (mtu_flag == 13)) + { + printf("\r\n ===========�ƶ�������ͨNB��===========!!\r\n"); + SIMCard_Type = 0; + } + else + { + printf("\r\n ===========SIM���쳣===========!!\r\n"); + SIMCard_Type = 2; + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Module_ME3616_menginfo_Check(void) +{ + gsdk_status_t status = GSDK_ERROR; + cell_info_t cell_info; + status = gsdk_ril_get_cell_info(&cell_info); + if(status == GSDK_SUCCESS) + { + printf("\r\n MU m_pci:%d \r\n", cell_info.m_sc.m_pci); //����ʹ�� + printf("\r\n MU m_rsrp:%d \r\n", cell_info.m_sc.m_rsrp); //����ʹ�� + printf("\r\n MU m_snr:%d \r\n", cell_info.m_sc.m_snr); //����ʹ�� + signalValue[0] = (cell_info.m_sc.m_pci) >> 8; //�����ϴ�ʱ��NB��С������С��PCI�Ÿ��ֽ� + signalValue[1] = cell_info.m_sc.m_pci; //�����ϴ�ʱ��NB��С������С��PCI�ŵ��ֽ� + signalValue[2] = (cell_info.m_sc.m_rsrp) >> 8; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[3] = cell_info.m_sc.m_rsrp; //�����ϴ�ʱ��NB��С��RSRPֵ���ֽ� + signalValue[4] = (cell_info.m_sc.m_snr) >> 8; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + signalValue[5] = cell_info.m_sc.m_snr; //�����ϴ�ʱ��NB��С��lart SNRֵ���ֽ� + } +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize) +{ + gsdk_status_t status = GSDK_ERROR; + PTU_flag = 0; + int lifetime = LIFETIME; +// char hex_user_data[MAX_HEX_SIZE] = {"001aa320001341201901092003092004860103ef88a380890a345f18"}; + int count = 0; + char srv_ip[MAX_IP_BYTE] = IPNET_T; + int srv_port = PORTNUM_T; + int i; + uint16_t k; + char SendArry[300] ={'\0'}; + + snprintf(SendArry,sizeof(SendArry),"00%.2x",iSize); + for(i=0;i 40) + { + DEBUG_LOG("wait for receive information timeout\r\n"); +// goto _fail; + break; + } + if (0 == strncmp(recv_buf, "+M2MCLIRECV:", 12)) + { + WARN_LOG("send data to iot is success\r\n"); + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + printf("\r\n send data count is : %d \r\n", count); + } + printf("\r\n send data final count is : %d \r\n", count); + + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + goto _fail; + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + goto _fail; + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + goto _fail; + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + + return PTU_flag; + +_fail: + /*delete link with iot*/ + status = gsdk_ril_del_link_iot(); + if (status == GSDK_ERROR) + { + DEBUG_LOG("delete link iot cmd failed\r\n"); + } + else if (status == GSDK_ERROR_TIMEOUT) + { + DEBUG_LOG("wait for information timeout,maybe signal bad\r\n"); + } + else if (status != GSDK_SUCCESS) + { + DEBUG_LOG("unknown error happen in delete link iot\r\n"); + } + else + { + WARN_LOG("delete iot is succcess\r\n"); + } + return 0; +} + +int ip_wait_func() +{ + int count = 0; + gsdk_status_t status; + char ip_buf[64] = {0}; + + while(1) + { + if(count > 20) + { + OEM_LOGE("wait for IP timeout\r\n"); + return -1; + } + memset(ip_buf, 0, 64); + status = gsdk_ril_get_local_ipaddr(ip_buf, 64); + if(status == 1) + { + OEM_LOGI("get ip success!\r\n"); + break; + } + OEM_LOGE("get ip failed!\r\n"); + vTaskDelay(500); + count++; + } + OEM_LOGI("ip registered\r\n"); + return 0; +} + +int gsdk_ril_init_func(void) +{ + gsdk_status_t status; + status = gsdk_ril_init(); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("gsdk ril init fail\r\n"); + return -1; + } + OEM_LOGI("gsdk ril init OK\r\n"); + return 0; +} + +int init_func() +{ + if(gsdk_ril_init_func() != 0) goto init_fail; + + OEM_LOGI("***************************************************\r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("* tcp demo \r\n"); + OEM_LOGI("* \r\n"); + OEM_LOGI("***************************************************\r\n"); + + OEM_LOGI("init_func end!\r\n"); + return 0; +init_fail: + OEM_LOGE("init_func error!\r\n"); + return -1; +} + +int create_socket_instance() +{ + socket_id=socket(AF_INET, SOCK_STREAM, 0); + if(socket_id < 0) + { + OEM_LOGE("socket create fail!\r\n"); + return -1; + } + OEM_LOGI("socket create success, socket id is : %d\r\n", socket_id); + return 0; +} + +int connect_tcp_server() +{ + struct sockaddr_in remaddr; + const char *server = DeviceConfig.ServerIP; + int server_port = DeviceConfig.ServerPort; + + if(PD2_Flag > 0) //��������������IP�ϴ�ʧ�ܣ���IP��ַ�Ͷ˿ں�����Ϊ203��ƽ̨ + { + server = IPNET_203; + server_port = PORTNUM_203; + printf("\n\r*--------------���ӱ��ݷ�����!---------------*\r\n"); + } + + /* get tcp server address and port */ + memset((char *) &remaddr, 0, sizeof(remaddr)); + remaddr.sin_family = AF_INET; + remaddr.sin_port = htons(server_port); + if (inet_aton(server, &remaddr.sin_addr)==0) + { + OEM_LOGE( "inet_aton func fail!\r\n"); + return -1; + } + if(0 > connect(socket_id, (struct sockaddr*)&remaddr, sizeof(remaddr))) + { + OEM_LOGE("connect tcp server fail!\r\n"); + perror("error code"); + OEM_LOGE("connect tcp server error, error number = %d\r\n", errno); + + return -1; + } + OEM_LOGI("connect tcp server success.\r\n"); + return 0; +} + +int tcp_send_demo(char* buf, unsigned short iSize) +{ + int len, ret; + + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &writefds); + FD_SET(socket_id, &errorfds); + maxfd = socket_id + 1; + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_send_demo start\r\n"); + /* monitor writefds and errorfds of socket */ + ret = select(maxfd, NULL, &writefds, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &writefds)) + { + if (send(socket_id, buf, iSize, 0) <= 0) + { + OEM_LOGE("tcp send fail\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp send success, send data is : %s\r\n", buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + return 0; +} + +int tcp_receive_demo() +{ + int len,ret; +// char recv_buf[500] = {0}; + int recvlen = 0; + + FD_ZERO(&readfds); + FD_ZERO(&errorfds); + FD_SET(socket_id, &readfds); + FD_SET(socket_id, &errorfds); + tv.tv_sec = 5; + tv.tv_usec = 0; + + OEM_LOGI("tcp_receive_demo start\r\n"); + /* monitor readfds and errorfds of socket */ + ret = select(maxfd, &readfds, NULL, &errorfds, &tv); + if((ret > 0) && FD_ISSET(socket_id, &errorfds)) + { + if(getsockopt(socket_id, SOL_SOCKET, SO_ERROR, &error_code, &len) == 0) + { + /* please refer to the arch.h of error_code's definition */ + OEM_LOGE("get error_code success : %d\r\n", error_code); + } + } + if((ret > 0) && FD_ISSET(socket_id, &readfds)) + { + recvlen = recv(socket_id, recv_buf, 500, 0); + if(recvlen <= 0) + { + OEM_LOGE("tcp receive error\r\n"); + return -1; + } + else + { + OEM_LOGI("tcp receive success, receive data is : %s\r\n", recv_buf); + PTU_flag = Receive_Data_Analysis(recv_buf); + } + } + if(ret == 0) + { + OEM_LOGE("select timeout.\r\n"); + return -1; + } + else if(ret < 0) + { + perror("error code"); + OEM_LOGE("select error, error number = %d\r\n", errno); + return -1; + } + + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize) +{ + PTU_flag = 0; + + /* system init */ +// if(init_func() != 0) goto end; + + /* wait for register network */ + if(ip_wait_func() != 0) goto end; + + /* create tcp socket instance */ + if(create_socket_instance() != 0) goto end; + + vTaskDelay(1 * 1000 / portTICK_PERIOD_MS); + + /* connect tcp server */ + if(connect_tcp_server() != 0) goto end; + + /* send data to tcp server */ + if(tcp_send_demo((char*)Psend, iSize) != 0) goto end; + + /* receive data from tcp server */ + if(tcp_receive_demo() != 0) goto end; + + /* close socket */ + if(socket_id >= 0) + { + OEM_LOGI("tcp send and receive finish, close socket now.\r\n"); + close(socket_id); + } + + OEM_LOGI("tcp demo finish!\r\n"); + return PTU_flag; + +end: + + OEM_LOGE("tcp demo abnormal exit!\r\n"); + if(socket_id >= 0) + { + OEM_LOGI("tcp demo error, close socket now.\r\n"); + close(socket_id); + } + + return -1; +} \ No newline at end of file diff --git a/NB-IOT.h b/NB-IOT.h new file mode 100644 index 0000000..122829f --- /dev/null +++ b/NB-IOT.h @@ -0,0 +1,43 @@ +#ifndef _NB_IOT_H_ +#define _NB_IOT_H_ +#include "AiderProtocol.h" +#include "config.h" +#define NETERRORCOUNT 3 +#define NETLOGIN 1 //�豸ע���վ +#define TCPCONNECT 2 //�豸����TCP���� +#define DATARECEV 3 //�豸���շ��������� +#define NETMODE 4 //������ʽ +#define NETSIGNAL 5 //�����ź�ǿ�� +#define MESSAGECONSULT 6 //��ѯ���� +#define NETSWITCH 7 //��ʽ�л� +#define PSM 8 //PSM +#define SCRAMBLING 9 //�������� +#define CPIN 10 //SIM��� +#define ZPAS 11 //������ +#define ESOC 12 //�����׽��� +#define CEREG 13 //ע���վ +#define ESOSENDRAW 14 //�������ݱ�� +#define M2MCLIDEL 16 //ƽ̨ȥע�� +#define M2MCLISEND 17 //���ݷ���״̬ +#define IMEICODE 18 //IMEI�� +#define ICCIDCODE 19 //ICCID�� +#define MENGINFOCode 20 //MENGINFO +#define NETSEARCHCOUNT 90 //����������� + +#define SENDBUFF_SIZE 300 //��?�䨮65535 +#define RECEIVEBUFF_SIZE 300 //��?�䨮65535 + +void Code_Read_NB(void); +void str_to_hex(char *out, const char *in, int len); +void Module_ME3616_menginfo_Check(void); +uint16_t SendMessage_NB_T(uint8_t* Psend, unsigned short iSize); +int ip_wait_func(); +int gsdk_ril_init_func(void); +int init_func(); +int create_socket_instance(); +int connect_tcp_server(); +int tcp_send_demo(char* buf, unsigned short iSize); +int tcp_receive_demo(); +uint16_t SendMessage_NB_MU(uint8_t* Psend, unsigned short iSize); +#endif + diff --git a/TEA.c b/TEA.c new file mode 100644 index 0000000..d880d6b --- /dev/null +++ b/TEA.c @@ -0,0 +1,164 @@ +/********************************************************************* +* TEA算法主文件 +* (c)copyright 2013,jdh +* All Right Reserved +*文件名:hash.c +*程序员:jdh +**********************************************************************/ + +/********************************************************************* +* 头文件 +**********************************************************************/ + +#include "TEA.h" +#include "stdio.h" +#include "string.h" +/********************************************************************* +* 函数 +**********************************************************************/ + +/********************************************************************* +* tea加密 +*参数:v:要加密的数据,长度为8字节 +* k:加密用的key,长度为16字节 +**********************************************************************/ + +void tea_encrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* tea解密 +*参数:v:要解密的数据,长度为8字节 +* k:解密用的key,长度为16字节 +**********************************************************************/ + +void tea_decrypt(uint32_t *v,uint32_t *k) +{ + uint32_t y = v[0],z = v[1],sum = 0xC6EF3720,i; + uint32_t delta = 0x9e3779b9; + uint32_t a = k[0],b = k[1],c = k[2],d = k[3]; + + for (i = 0; i < 32; i++) + { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + v[0] = y; + v[1] = z; +} + +/********************************************************************* +* 加密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:密文的字节数 +**********************************************************************/ + +uint16_t encrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint8_t a = 0; + uint16_t i = 0; + uint16_t num = 0; + + //将明文补足为8字节的倍数 + a = size_src % 8; + if (a != 0) + { + for (i = 0; i < 8 - a; i++) + { + src[size_src++] = 0; + } + } + + //加密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_encrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + +/********************************************************************* +* 解密算法 +*参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 +* size_src:源数据大小,单位字节 +* key:密钥,16字节 +*返回:明文的字节数,如果失败,返回0 +**********************************************************************/ + +uint16_t decrypt(uint8_t *src,uint16_t size_src,uint8_t *key) +{ + uint16_t i = 0; + uint16_t num = 0; + + //判断长度是否为8的倍数 + if (size_src % 8 != 0) + { + printf("\r\n 数据长度不是8字节的倍数!!"); + return 0; + } + + //解密 + num = size_src / 8; + for (i = 0; i < num; i++) + { + tea_decrypt((uint32_t *)(src + i * 8),(uint32_t *)key); + } + + return size_src; +} + + +uint16_t Tea_Encrypt(uint8_t *src,uint16_t size_src) +{ + unsigned char TEA_key[16]; + unsigned char i=0,size; + memcpy(TEA_key,TEA_KEY,TEA_KEY_LEN);//做运算之前先要设置好密钥。 + + printf("\r\n==============================================\r\n"); + printf("\r\ndat:"); + for(i=0; i +#include +#include "gsdk_api.h" + +#include "bsp_ds2782.h" + +#include "gsdk_sdk.h" + +static gsdk_handle_t g_hi2c; + +/* +* ������ Write_DS2782_Shadow_RAM +* ���� дDS2782Ӱ�ӼĴ��� +*/ +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = reg_Addres; + data_reg_addr[1] = data; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error1 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error2 (%d)\n", ret); + } +} + + +/* +* ������ Copy_Data_For_Shadow_RAM_To_EEPROM +* ���� ��Ӱ�ӼĴ�������Copy��EEPROM +* ��������ֲ� P26ҳ Function Command Protocol �½� +*/ +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK) +{ + int ret; + uint8_t data_reg_addr[] = {0x00, 0x00}; + data_reg_addr[0] = FCR; + data_reg_addr[1] = EEPROM_BLOCK; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[1], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error3 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error4 (%d)\n", ret); + } +} + +/* +* ������ Get_DS2782_STATUS +* ���� ��״̬�Ĵ��� +*/ +uint8_t Get_DS2782_STATUS() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[2]; + int timeout_ms = 5000; + + data_reg_addr[0] = STATUS; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address|I2C_WR, data_reg_addr, 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address|I2C_RD, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_Temperature +* ���� ��ȡDS2782�¶� �Ŵ�ʮ�� +*/ +uint16_t Get_DS2782_Temperature() +{ + uint8_t data[2]; + uint16_t Temperature=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = TEMPMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + Temperature+=data[0]<<3; + + Temperature+=data[1]>>5; + + Temperature*=0.125; + + return Temperature; +} + +/* +* ������ Get_DS2782_Voltage +* ���� ��ȡDS2782��ѹ +*/ +uint16_t Get_DS2782_Voltage() +{ + uint8_t data[2]; + uint16_t Voltage=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = VOLTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Voltage+=data[0]<<3; + + Voltage+=data[1]>>5; + + Voltage*=4.88; + + Voltage*=2; + + return Voltage; +} + +/* +* ������ Get_DS2782_Current +* ���� ��ȡDS2782���� +*/ +int16_t Get_DS2782_Current() +{ + uint8_t data[2]; + int16_t Current=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = CURRENTMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Current+=data[0]<<8; + + Current+=data[1]; + + Current=(int)(Current*1.5625/RSNSP_mOhm); //mA + + return Current; +} + +/* +* ������ Get_DS2782_RAAC +* ���� ʣ�������Ч���� (RAAC) [mAh] +*/ +uint16_t Get_DS2782_RAAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RAACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + +/* +* ������ Get_DS2782_RSAC +* ���� ʣ����Դ������� (RSAC) [mAh] +*/ +uint16_t Get_DS2782_RSAC() +{ + uint8_t data[2]; + uint16_t Remaining_Capacity=0; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSACMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + Remaining_Capacity+=data[0]<<8; + + Remaining_Capacity+=data[1]; + + return Remaining_Capacity; +} + + +/* +* ������ Get_DS2782_RARC +* ���� ʣ����Զ���Ч���� (RARC) [%] +*/ +uint8_t Get_DS2782_RARC() +{ + uint8_t data; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = RARC; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + +/* +* ������ Get_DS2782_RSRC +* ���� ʣ����Դ������� (RSRC) [%] +*/ +uint8_t Get_DS2782_RSRC() +{ + uint8_t data; + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = RSRC; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, &data, 1, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + return data; +} + + +/* +* ������ Get_DS2782_ACR +* ���� . �ۼƵ��� (ACRMSB) [6.25��Vh/RSNS] +*/ +uint16_t Get_DS2782_ACR() +{ + uint8_t data[2]; + uint16_t ACR=0; + + int ret; + uint8_t data_reg_addr[] = {0x00}; + int timeout_ms = 5000; + + data_reg_addr[0] = ACRMSB; + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error (%ld)\n", ret); + OEM_LOGE("I2C Transmit error (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error (%ld)\n", ret); + OEM_LOGE("I2C Read error (%d)\n", ret); + return -1; + } + + ACR+=data[0]<<8; + + ACR+=data[1]; + + ACR=(int)(ACR*0.625); + + return ACR; +} + +/* +* ������ Get_DS2782_FULL(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_FULL() +{ + uint8_t data[2]; + uint16_t full=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = FULLMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + full+=data[0]<<8; + + full+=data[1]; + + return full; +} + +/* +* ������ Get_DS2782_AE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_AE() +{ + uint8_t data[2]; + uint16_t AE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + AE+=data[0]<<8; + + AE+=data[1]; + + return AE; +} + +/* +* ������ Get_DS2782_SE(T) +* ���� ��ȡDS2782��ǰ�¶��������� +*/ +uint16_t Get_DS2782_SE() +{ + uint8_t data[2]; + uint16_t SE=0; + int ret; + int timeout_ms = 5000; + uint8_t data_reg_addr[] = {0x00}; + data_reg_addr[0] = AEMSB; + + ret = gsdk_i2c_write(g_hi2c, DS2782_8bit_Slave_Address, &data_reg_addr[0], 1); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Transmit error5 (%ld)\n", ret); + OEM_LOGE("I2C Transmit error6 (%d)\n", ret); + return -1; + } + + ret = gsdk_i2c_read(g_hi2c, DS2782_8bit_Slave_Address, data, 2, timeout_ms); + if (ret != GSDK_SUCCESS) + { + gsdk_syslog_printf("I2C Read error7 (%ld)\n", ret); + OEM_LOGE("I2C Read error8 (%d)\n", ret); + return -1; + } + + SE+=data[0]<<8; + + SE+=data[1]; + + return SE; +} + +void bsp_Init_DS2782() +{ + gsdk_status_t status; + + //��ʼ��I2C���� + status = gsdk_i2c_master_init(I2C_MASTER_2, I2C_FREQUENCY_400K, &g_hi2c);//welinkopen only support i2c master 2 or i2c master 1 + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[PRINTF_DEMO]: failed to open i2c %d\n", status); + } + else + { + printf("****************************i2c open success*********************************\n"); + } + + +} + +void Module_DS2781_Set_Register(void) +{ + uint8_t TEMP[2]; + uint16_t TEMP_Word; + + /* ���� Parameter EEPROM Memory Block 1 �� shadow RAM ��Ӱ�ӼĴ����� */ + + Write_DS2782_Shadow_RAM(CONTROL,0x00); //���ƼĴ��� + + Write_DS2782_Shadow_RAM(AB,0x00); //�ۼ�ƫ�� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� �ϻ����� AC ��λ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + Write_DS2782_Shadow_RAM(ACMSB,TEMP[0]); //�ϻ����� AC + Write_DS2782_Shadow_RAM(ACLSB,TEMP[1]); //�ϻ����� AC + + /*���� ����ѹVCHG AC ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Charge_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VCHG,TEMP[0]); //����ѹVCHG + + /*д��AS��100% */ + TEMP[0]= 0x80; + + Write_DS2782_Shadow_RAM(AS,TEMP[0]); //AS + + /*���� ��С������ IMIN ��λ Ϊ50��V */ + TEMP[0]=(int)(Minimum_Charge_Current*RSNSP_mOhm/50); + + Write_DS2782_Shadow_RAM(IMIN,TEMP[0]); //��С������ IMIN + + + /*���� VAE 0x66 ��Ч�յ�ѹ ��λ Ϊ19.52mV */ + TEMP[0]=(int)(Empty_Voltage/19.52); + + Write_DS2782_Shadow_RAM(VAE,TEMP[0]); // VAE 0x66 ��Ч�յ�ѹ + + + /*���� IAE 0x67 ��Ч�յ��� ��λ Ϊ200��V */ + TEMP[0]=(int)(Empty_Current*RSNSP_mOhm/200); + + Write_DS2782_Shadow_RAM(IAE,TEMP[0]); // IAE 0x67 ��Ч�յ��� + + + /*���� RSNSP 0x69 �������� ��λ �絼��ʽ�洢 */ + TEMP[0]=(int)(1000/RSNSP_mOhm); + + Write_DS2782_Shadow_RAM(RSNSP,TEMP[0]); // RSNSP 0x69 �������� + + + TEMP_Word=(uint16_t)Capacity*RSNSP_mOhm; //���� +40��C�¶�ʱ��������ֵ 6.25��Vh + TEMP_Word=(int)(TEMP_Word/6.25); + + TEMP[0]=TEMP_Word>>8; + TEMP[1]=TEMP_Word&0xFF; + + /* + Write_DS2782_Shadow_RAM(FULLSMSB,TEMP[0]); //+40��C�¶�ʱ��������ֵ FULLS + Write_DS2782_Shadow_RAM(FULLSLSB,TEMP[1]); //+40��C�¶�ʱ��������ֵ FULLS + + Write_DS2782_Shadow_RAM(0x68, 0x06); + Write_DS2782_Shadow_RAM(FULL3040,0x0E); // ������б�� + Write_DS2782_Shadow_RAM(FULL2030,0x11); // ������б�� + Write_DS2782_Shadow_RAM(FULL1020,0x31); // ������б�� + Write_DS2782_Shadow_RAM(FULL0010,0x36); // ������б�� + + Write_DS2782_Shadow_RAM(AE3040,0x05); // �յ���б�� + Write_DS2782_Shadow_RAM(AE2030,0x09); // �յ���б�� + Write_DS2782_Shadow_RAM(AE1020,0x12); // �յ���б�� + Write_DS2782_Shadow_RAM(AE0010,0x25); // �յ���б�� + + Write_DS2782_Shadow_RAM(SE3040,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE2030,0x03); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE1020,0x05); // �����յ���б�� + Write_DS2782_Shadow_RAM(SE0010,0x15); // �����յ���б�� + + Write_DS2782_Shadow_RAM(RSGAINMSB, 0x04); + Write_DS2782_Shadow_RAM(RSGAINLSB, 0x08); + */ + + Copy_Data_For_Shadow_RAM_To_EEPROM(Copy_data_back_1); + printf("\r\n ���ؼ����óɹ�����\r\n"); +} diff --git a/bsp_ds2782.h b/bsp_ds2782.h new file mode 100644 index 0000000..e859af6 --- /dev/null +++ b/bsp_ds2782.h @@ -0,0 +1,193 @@ +/* +********************************************************************************************************* +* +* ģ������ : ds2782���� +* �ļ����� : DS2782.h +* �� �� : V1.0 +* ˵ �� : +* +* �޸ļ�¼ : +* �汾�� ���� ���� ˵�� +* V1.0 2019-02-022 Baiyang ��ʽ���� +* +* Copyright (C), ������������ +* +********************************************************************************************************* +*/ + +#ifndef __BSP_DS2782_H +#define __BSP_DS2782_H + +/* Function Commands */ +/* ��ϸ Memory Map �������ֲ�P27 [Table 5. Function Commands] */ +#define Copy_data_back_0 0x42 +#define Copy_data_back_1 0x44 +#define recall_data_back_0 0xB2 +#define recall_data_back_1 0xB4 +#define lock_data_back_0 0x63 +#define lock_data_back_1 0x66 + +/* Memory Map */ +/* ��ϸ Memory Map �������ֲ�P23 */ +#define STATUS 0x01 +#define RAACMSB 0x02 +#define RAACLSB 0x03 +#define RSACMSB 0x04 +#define RSACLSB 0x05 +#define RARC 0x06 +#define RSRC 0x07 +#define IAVGMSB 0x08 +#define IAVGLSB 0x09 +#define TEMPMSB 0x0A +#define TEMPLSB 0x0B +#define VOLTMSB 0x0C +#define VOLTLSB 0x0D +#define CURRENTMSB 0x0E +#define CURRENTLSB 0x0F +#define ACRMSB 0x10 //. �ۼƵ��� +#define ACRLSB 0x11 +#define ACRLMSB 0x12 +#define ACRLLSB 0x13 +#define AS 0x14 +#define SFR 0x15 +#define FULLMSB 0x16 +#define FULLLSB 0x17 +#define AEMSB 0x18 +#define AELSB 0x19 +#define SEMSB 0x1A +#define SELSB 0x1B + +#define CONTROL 0x60 //���ƼĴ��� +#define AB 0x61 //�ۼ�ƫ�� +#define ACMSB 0x62 //�ϻ����� +#define ACLSB 0x63 +#define VCHG 0x64 //����ѹ +#define IMIN 0x65 //��С������ +#define VAE 0x66 //��Ч�յ�ѹ +#define IAE 0x67 //��Ч�յ�9999�� +#define RSNSP 0x69 //�������� +#define FULLSMSB 0x6A //+40��C�¶�ʱ��������ֵ +#define FULLSLSB 0x6B +#define FULL3040 0x6C +#define FULL2030 0x6D +#define FULL1020 0x6E +#define FULL0010 0x6F +#define AE3040 0x70 +#define AE2030 0x71 +#define AE1020 0x72 +#define AE0010 0x73 +#define SE3040 0x74 +#define SE2030 0x75 +#define SE1020 0x76 +#define SE0010 0x77 +#define RSGAINMSB 0X78 +#define RSGAINLSB 0X79 +#define RSTC 0X7A +#define FCR 0XFE + +#define I2C_WR 0X00 +#define I2C_RD 0X01 + + +/* +*Slave Address +* 7bit 0110100 +*/ +#define DS2782_8bit_Slave_Address 0x34 + +#define Capacity 4900 //������� mAH +#define RSNSP_mOhm 10 //�������� ��ŷ +#define Charge_Voltage 3600 //����ѹ mV +#define Minimum_Charge_Current 20 //��С������ mA +#define Empty_Voltage 2800 //��Ч�յ��ѹ mV +#define Empty_Current 300 //��Ч�յ���� mA + +typedef struct +{ +uint8_t _STATUS ; //0x01 +uint8_t _RAACMSB ; //0x02 +uint8_t _RAACLSB ; //0x03 +uint8_t _RSACMSB ; //0x04 +uint8_t _RSACLSB ; //0x05 +uint8_t _RARC ; //0x06 +uint8_t _RSRC ; //0x07 +uint8_t _IAVGMSB ; //0x08 +uint8_t _IAVGLSB ; //0x09 +uint8_t _TEMPMSB ; //0x0A +uint8_t _TEMPLSB ; //0x0B +uint8_t _VOLTMSB ; //0x0C +uint8_t _VOLTLSB ; //0x0D +uint8_t _CURRENTMSB ; //0x0E +uint8_t _CURRENTLSB ; //0x0F +uint8_t _ACRMSB ; //0x10 //. �ۼƵ��� +uint8_t _ACRLSB ; //0x11 +uint8_t _ACRLMSB ; //0x12 +uint8_t _ACRLLSB ; //0x13 +uint8_t _AS ; //0x14 +uint8_t _SFR ; //0x15 +uint8_t _FULLMSB ; //0x16 +uint8_t _FULLLSB ; //0x17 +uint8_t _AEMSB ; //0x18 +uint8_t _AELSB ; //0x19 +uint8_t _SEMSB ; //0x1A +uint8_t _SELSB ; //0x1B +}Ds278x; + + +typedef struct +{ + uint8_t _STATUS; + uint16_t _RAAC; //ʣ�������Ч���� (RAAC) [mAh] + uint16_t _RSAC; + uint8_t _RARC; //ʣ�������Ч���� (RARC) [%] + uint8_t _RSRC; + uint16_t _IAVG; + uint16_t _TEMP; + uint16_t _VOLT; + + int16_t Current; + uint16_t Voltage; + + uint16_t _ARC; + + uint8_t Connect; //оƬ����״̬ ������д��1 ����ʧ��д�� 0 + +}Ds2782_Typedef; + + +extern Ds2782_Typedef Ds2782; + +extern Ds278x Ds2782_RAM; + +uint8_t Get_DS2782_STATUS(); + +uint16_t Get_DS2782_Temperature(); + +uint16_t Get_DS2782_Voltage(); + +int16_t Get_DS2782_Current(); + +uint16_t Get_DS2782_RAAC(); + +uint16_t Get_DS2782_RSAC(); + +uint8_t Get_DS2782_RARC(); + +uint8_t Get_DS2782_RSRC(); + +uint16_t Get_DS2782_ACR(); + +uint16_t Get_DS2782_FULL(); +uint16_t Get_DS2782_AE(); +uint16_t Get_DS2782_SE(); + + +void Write_DS2782_Shadow_RAM(uint8_t reg_Addres,uint8_t data); + +void Copy_Data_For_Shadow_RAM_To_EEPROM(uint8_t EEPROM_BLOCK); + +void bsp_Init_DS2782(); + +void Module_DS2781_Set_Register(void); + +#endif \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..f39d897 --- /dev/null +++ b/build.bat @@ -0,0 +1,73 @@ +@echo off & setlocal enabledelayedexpansion +set PATH=C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;%CD%\..\tools;%PATH% +for /l %%i in (0,1,50) do ( + +for /f "delims=" %%a in ('findstr /n .* C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config_base.h') do ( + set "str=%%a" + if not "!str:0x34,0x20=!"=="%%a" ( + rem if not defined n ( + for /f "tokens=5,6 delims=," %%b in ("%%a") do ( set /a high=%%b,low=%%c) + for /f "tokens=5 delims=," %%b in ("%%a") do ( set higho=%%b) + for /f "tokens=6 delims=," %%b in ("%%a") do ( set lowo=%%b) + rem for /f "tokens=5 delims=," %%c in ("%%a") do set "high=%%c" + set /a lown = !low! + %%i + set /a highn = !lown!/256 +!high! , lown = !lown!%% 256 + set /a lown_l = !lown! %% 16 + set /a lown_h = !lown! /16 + set /a highn_l = !highn! %% 16 + set /a highn_h = !highn! /16 + set /a assembl_falg=0 + + if !lown_l! == 10 set /a assembl_falg= 1 + if !lown_l! == 11 set /a assembl_falg= 1 + if !lown_l! == 12 set /a assembl_falg= 1 + if !lown_l! == 13 set /a assembl_falg= 1 + if !lown_l! == 14 set /a assembl_falg= 1 + if !lown_l! == 15 set /a assembl_falg= 1 + + if !lown_h! == 10 set /a assembl_falg= 1 + if !lown_h! == 11 set /a assembl_falg= 1 + if !lown_h! == 12 set /a assembl_falg= 1 + if !lown_h! == 13 set /a assembl_falg= 1 + if !lown_h! == 14 set /a assembl_falg= 1 + if !lown_h! == 15 set /a assembl_falg= 1 + + if !highn_l! == 10 set /a assembl_falg= 1 + if !highn_l! == 11 set /a assembl_falg= 1 + if !highn_l! == 12 set /a assembl_falg= 1 + if !highn_l! == 13 set /a assembl_falg= 1 + if !highn_l! == 14 set /a assembl_falg= 1 + if !highn_l! == 15 set /a assembl_falg= 1 + + if !highn_h! == 10 set /a assembl_falg= 1 + if !highn_h! == 11 set /a assembl_falg= 1 + if !highn_h! == 12 set /a assembl_falg= 1 + if !highn_h! == 13 set /a assembl_falg= 1 + if !highn_h! == 14 set /a assembl_falg= 1 + if !highn_h! == 15 set /a assembl_falg= 1 + rem ) + rem echo low = !low! + rem echo lown = !lown! + rem echo lowo = !lowo! + for /f "tokens=1,2" %%d in ("!lowo! !lown!") do set "str=!str:%%d=%%e!" + for /f "tokens=1,2" %%g in ("!higho! !highn!") do set "str=!str:%%g=%%h!" + ) + echo,!str:*:=! + )>>tmp + +move tmp C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h +echo -----------------------!highn_h!!highn_l!!lown_h!!lown_l!----------------------- + :: echo !assembl_falg! + rem if "%lown_l%"="A" echo good +if !assembl_falg!==0 ( + + echo ====================Init building !highn_h!!highn_l!!lown_h!!lown_l!...==================== + +make + + type C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\config.h + ren C:\Users\think\Desktop\sdk3\examples\BIRMM-GT200N_V1.14\bin\user.bin BIRMM-GT200N-34202401!highn_h!!highn_l!!lown_h!!lown_l!.bin +)>>assemble_log.txt + +) +pause diff --git a/config.h b/config.h new file mode 100644 index 0000000..96a6095 --- /dev/null +++ b/config.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ diff --git a/config_base.h b/config_base.h new file mode 100644 index 0000000..9f0bfcc --- /dev/null +++ b/config_base.h @@ -0,0 +1,26 @@ +/******************************************************************************* + ����ǰ����ظ���Ŀ����ȷ�������������� +********************************************************************************/ + +/**************************������ʽƽ̨*****************************************/ +#define IPNET_T {"117.60.157.137"} //����IP +#define PORTNUM_T 5683 //�˿ں� +/*******************************************************************************/ + +/**************************�û�ƽ̨��������ͨ���ƶ�*****************************/ +#define IPNET_MAIN "223.82.47.232" //������IP +#define PORTNUM_MAIN 11521 //���˿ں� +/*******************************************************************************/ + +/**************************203��ƽ̨�����������ϴ���������ͨ���ƶ�**************/ +#define IPNET_203 "139.198.18.188" //������IP +#define PORTNUM_203 2019 //���˿ں� +/*******************************************************************************/ + +/**************************�豸ID��*********************************************/ +#define DEVICE_ID {0x34,0x20,0x22,0x01,0x00,0x70,0x00} +/*******************************************************************************/ + +/**************************����汾��*******************************************/ +#define SOFTWARE_VERSION "BIRMM-GT200N_V1.15" +/*******************************************************************************/ \ No newline at end of file diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..7830fc4 --- /dev/null +++ b/flash.c @@ -0,0 +1,105 @@ +/* +** File : flash.c +** +** Copyright (C) 2013-2019 Gosuncn. All rights reserved. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +** +** Author : lijiquan@gosuncn.cn +** +** $Date: 2018/11/01 08:45:36GMT+08:00 $ +** +*/ + +#include +#include +#include +#include "gsdk_api.h" +#include "flash.h" +#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); + +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length) +{ + gsdk_status_t status; + gsdk_handle_t hflash; + int i; + + for(i=0; i<5; i++) + { + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status == GSDK_SUCCESS) + { + printf("Flash open success!\n"); + break; + } + else{ + printf("Flash open failed!\n"); + } + } + + for(i=0; i<5; i++) + { + status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length); + if (status == GSDK_SUCCESS) + { + printf("Flash read success!\n"); + break; + } + else{ + printf("Flash read failed!\n"); + } + } + + gsdk_flash_close(hflash); + if (status == GSDK_SUCCESS) + return -1; + else + return 0; +} + +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length) +{ + char buf[2560]; + gsdk_status_t status; + gsdk_handle_t hflash; + + status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash); + if (status != GSDK_SUCCESS) + goto _fail; + + status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + { + goto _fail_close; + } + + memcpy(buf + address, data, length); + + status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K); + if (status != GSDK_SUCCESS) + goto _fail_close; + + status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560); + if (status != GSDK_SUCCESS) + goto _fail_close; + + gsdk_flash_close(hflash); + return -1; + +_fail_close: + DEBUG_LOG("Flash test failed status is %d\r\n", status); + gsdk_flash_close(hflash); +_fail: + printf("[FLASH_DEMO] Flash test failed!\n"); + return 0; +} \ No newline at end of file diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..d55ac11 --- /dev/null +++ b/flash.h @@ -0,0 +1,34 @@ +#ifndef _FLASH_H_ +#define _FLASH_H_ + +/////////////////////////////FLASH///////////////////////////// +#define FLASH_TEST_BASE 0x002DB000 +#define FLASH_TEST_SIZE 0x1000 + +#define RUN_COUNT_ADDR 0x002DB000 //�豸�������� +#define CollectPeriod_ADDR 0x002DB080 //�ɼ��������λ���� +#define CollectNum_ADDR 0x002DB100 //ÿ�������ϱ��ɼ������ݸ��� +#define UploadCycle_ADDR 0x002DB180 //�����ϱ����� +#define CollectTime_ADDR 0x002DB200 //�ɼ�ʱ�䣬��λ���� +#define RetryNum_ADDR 0x002DB280 //�ش����� +#define CollectedDateCount_ADDR 0x002DB300 //�Ѳɼ������ݸ��� +#define HistoryDateCount_ADDR 0x002DB380 //δ���͵���ʷ���ݸ��� +#define ServerIP_ADDR 0x002DB400 //������IP +#define ServerPort_ADDR 0x002DB480 //�������˿� +#define BATT_InitCapacity_ADDR 0x002DB500 //��ص�����ʼ���� +#define MountHeight_ADDR 0x002DB580 //Һλ̽ͷ�����׵ĸ߶� +#define AlarmValue_ADDR 0x002DB600 //������ֵ +#define Date_ADDR 0x002DB680 //������ֵ +#define UpdateCount_ADDR 0x002DB700 //������ֵ +#define StartTime_ADDR 0x002DB780 //�ɼ��ĵ�һ�����ݵ�ʱ������ + +#define CollectedDate_ADDR 0x002DB800 //�Ѳɼ������� +#define ParameterReply_ADDR 0x002DB880 //�������ûظ� +#define HistoryDate_ADDR 0x002DB900 //δ���͵���ʷ���� +#define SendCount_ADDR 0x002DB980 //δ���͵Ĵ��� + +//�������� +uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length); +uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length); +#endif + diff --git a/gprs.c b/gprs.c new file mode 100644 index 0000000..29461b5 --- /dev/null +++ b/gprs.c @@ -0,0 +1,754 @@ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "sleep.h" +#include "API-Platform.h" +#include "sleep.h" +#include "TEA.h" +#include "NB-IOT.h" +#include "gsdk_sdk.h" +#include +#include "flash.h" +#include "AiderProtocol.h" +#include "gsdk_ril.h" +#include "gsdk_ril_cmds.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include + +extern struct Config_RegPara ConfigData; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +extern struct DeviceSet DeviceConfig; //Һλ��������Ϣ�ṹ�� +extern struct DataFrame dataFrame; +extern uint8_t SIMCard_Type; +extern struct BusinessData businessData; + +uint8_t Flash_Write_Flag[9]; + +// �Զ����tolower������ +int _tolower(int c) +{ + if (c>='A' && c<='Z') return c+32; + else return c; +} + + +/******************************************************************************* +* Function Name : int string_to_hex(char* h,char s[]) +* Description : string��a?��3��hex��?2�顤��??hex��a?����y��? ?��a320?��-->0xa3,0x20 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int string_to_hex(char* h,char s[]) +{ + int i ,j; + + for (i=0,j=0; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z'); ++i) + { + if (_tolower(s[i]) > '9') + { + h[j] = 16 * h[j] + (10 + _tolower(s[i]) - 'a'); + } + else + { + h[j] = 16 * h[j] + (_tolower(s[i]) - '0'); + } + if(i%2==1) + { + j++; + } + } + return j; + +} + + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned char ReverseBitOrder08( unsigned char iSrc ) +{ + unsigned char index; + unsigned char iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X07; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short ReverseBitOrder16( unsigned short iSrc ) +{ + unsigned char index; + unsigned short iDst; + + iDst = iSrc & 0X01; + for( index = 0X00; index < 0X0F; index++ ) + { + iDst <<= 0X01; + iSrc >>= 0X01; + iDst |= ( iSrc & 0X01 ); + } + return iDst; +} + +/******************************************************************************* +* Function Name : unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +* Description : f(X)=X^16 + X^15 + X^2 + X^0��?POLYNOMIALS = 0X8005 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ) +{ + unsigned char index; + unsigned short iCRC; + + // The default value + iCRC = 0XFFFF; + while ( iSize-- ) + { + iCRC ^= ( ( ( unsigned short ) ReverseBitOrder08( *pMsg ) ) << 0X08 ); + for ( index = 0X00; index < 0X08; index++ ) + { + if ( iCRC & 0X8000 ) + { + iCRC = ( iCRC << 1 ) ^ 0X8005; + } + else + { + iCRC <<= 1; + } + } + pMsg++; + } + return ReverseBitOrder16( iCRC ); +} + +/***********�������ܣ����ض������з���һ��ָ��������****************/ +/***********����ҵ����򷵻�Ŀ��������Դ�����е��׵�ַ**************/ +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object) +{ + char* Ptemp1 = Source; + char* Ptemp2 = Object; + short i=0,j=0; + short count=0; + + if((Length_Source < 0)||(Length_Object < 0)) + { + return NULL; + } + if(Length_Source < Length_Object) + { + return NULL; + } + + else if(Length_Source == Length_Object) + { + if((Length_Source==0)&&(Length_Object==0)) + { + return NULL; + } + else + { + for(i=0; i0)&&(ParaRequest.OID_Count <=20)) //�޶�OID���ȣ���ֹ�ڴ���� + { + pOid = (uint32_t*)&pTreatyBuff[16]; + j =ParaRequest.OID_Count; + for(i=0; i6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + + printf("\r\nServer Receive Trap Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + break; + } + case DEF_NR: //�ش����� + { + DeviceConfig.RetryNum = *(pChar + 6); + if(DeviceConfig.RetryNum >= 1) //�����Ϸ����ж� + { + Flash_Write_Flag[0] = 1; +// Module_Flash_Write(RetryNum_ADDR - FLASH_TEST_BASE, &(DeviceConfig.RetryNum), 1); + } + else //�����ò������Ϸ�ʱ��ʹ��Ĭ�ϲ�����ͬʱ������Flash���� + { + DeviceConfig.RetryNum = 1; //Ϊ�˷�����ʵ���������Ĭ�ϲ��� + } + printf("\r\n-Retry Num-%x---.\r\n", DeviceConfig.RetryNum); //����ʹ�� + pOid = (uint32_t*)(pChar + 7); //ָ�����1��Tag + DataLen = DataLen - 7; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_STIME1: //һʱ���ɼ�ʱ�� + { + DeviceConfig.CollectTime = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectTime:-%2x---.\r\n", DeviceConfig.CollectTime ); //����ʹ�� + if(DeviceConfig.CollectTime <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[1] = 1; +// Module_Flash_Write( CollectTime_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectTime), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_ITRL1: //һʱ���ɼ���� + { + DeviceConfig.CollectPeriod = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectPeriod:-%2x---.\r\n", DeviceConfig.CollectPeriod ); //����ʹ�� + if(DeviceConfig.CollectPeriod <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[2] = 1; +// Module_Flash_Write(CollectPeriod_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectPeriod), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case CLT1_CNT1: //һʱ���ɼ����� + { + DeviceConfig.CollectNum = pChar[6] * 256 + pChar[7]; + printf("\r\n-CollectNum:-%2x---.\r\n", DeviceConfig.CollectNum ); //����ʹ�� + if(DeviceConfig.CollectNum <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[3] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case UPLOAD_CYCLE: //�ϱ����� + { + DeviceConfig.UploadCycle = pChar[6] * 256 + pChar[7]; + if(DeviceConfig.UploadCycle <= 1440) //�����Ϸ����ж� + { + Flash_Write_Flag[4] = 1; +// Module_Flash_Write(UploadCycle_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.UploadCycle), 2); + } + printf("\r\n-UploadCycle:-%2x---.\r\n", DeviceConfig.UploadCycle ); //����ʹ�� + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case MOUNT_HEIGHT: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.MountHeight.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.MountHeight.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[5] = 1; +// Module_Flash_Write(MountHeight_ADDR - FLASH_TEST_BASE, DeviceConfig.MountHeight.Data_Hex, 4); + } + printf("\r\n-MountHeight:-%f---.\r\n", DeviceConfig.MountHeight.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case ALARM_VALUE: //��Ũ�ȱ�����ֵ + { + for(k = 0; k < 4; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + DeviceConfig.AlarmValue.Data_Hex[k] = pChar[k+6]; + } + if(DeviceConfig.AlarmValue.Data_Float <= 100) //�����Ϸ����ж� + { + Flash_Write_Flag[6] = 1; +// Module_Flash_Write(AlarmValue_ADDR - FLASH_TEST_BASE, DeviceConfig.AlarmValue.Data_Hex, 4); + } + printf("\r\n-AlarmValue:-%f---.\r\n", DeviceConfig.AlarmValue.Data_Float); //����ʹ�� + pOid = (uint32_t*)(pChar + 10); //ָ�����1��Tag + DataLen = DataLen - 10; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + case BSS_IP: //�������IP��ַ����IP�� + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 6) && (Tag_Lenth < 16)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else if((pChar[6 + k] == '.') && (pChar[6 + k - 1] != '.')) //�ָ���ͳ�� + { + Dot_Counter++; + DeviceConfig.ServerIP[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(Dot_Counter == 3) + { + DeviceConfig.ServerIP[k] = '\0'; + Flash_Write_Flag[7] = 1; +// Module_Flash_Write(ServerIP_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.ServerIP), k); + printf("\r\n���պϷ�IP\r\n "); + } + else + { + printf("\r\n���շǷ�IP\r\n "); + } + + printf("\r\n-��IP��:-%s---.\r\n", DeviceConfig.ServerIP ); //����ʹ�� + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { +#if DEBUG_TEST + printf("\r\n ��IP��ʽ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + } + case BSS_PORT: //�������˿ں� + { + DeviceConfig.ServerPort = pChar[6] * 256 + pChar[7]; + printf("\r\n-ServerPort:-%d---.\r\n", DeviceConfig.ServerPort ); //����ʹ�� + if(DeviceConfig.ServerPort < 65535) //�����Ϸ����ж� + { + Flash_Write_Flag[8] = 1; +// Module_Flash_Write(CollectNum_ADDR - FLASH_TEST_BASE, (uint8_t*) & (DeviceConfig.CollectNum), 2); + } + pOid = (uint32_t*)(pChar + 8); //ָ�����1��Tag + DataLen = DataLen - 8; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + break; + } + + { + Tag_Lenth = *(pChar + 5); + if((Tag_Lenth > 0) && (Tag_Lenth < 6)) + { + for(k = 0; k < Tag_Lenth; k++) //���ú����Ѿ����˷��������,strlen(pSetPara)<=15 + { + if((pChar[6 + k] >= '0') && (pChar[6 + k] <= '9')) + { + ServerPort[k] = pChar[6 + k]; + } + else + { + break; + } + } + + if(k == Tag_Lenth) + { + ServerPort[k] = '\0'; + Flash_Write_Flag[8] = 1; + DeviceConfig.ServerPort = atoi((char*)ServerPort); + printf("\r\n���պϷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 6 + Tag_Lenth); //ָ�����1��Tag + DataLen = DataLen - 6 - Tag_Lenth; + i++; //�Ϸ�OID������ + dataFrame.Tag_Count++; + } + else + { + printf("\r\n���շǷ��˿ں�\r\n "); + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + printf("\r\n-���˿ں�:-%s---.\r\n", ServerPort); //����ʹ�� + break; + } + else + { +#if DEBUG_TEST + printf("\r\n ��PORT��Χ����ȷ!\r\n"); //����ʹ�� +#endif + pOid = (uint32_t*)(pChar + 1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen - 1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + } + break; + + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Trap Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + break; + } + case 0x099C: //Startup Response + { +#if DEBUG_TEST + printf("\r\nReceive Startup Response Command from Server.\r\n"); //����ʹ�� +#endif + DataLen =pTreatyBuff[2]*256 +pTreatyBuff[3]-12; //���յ���Tag���е��ܳ��� + if(DataLen >6) //���ٴ���һ���Ϸ������ò��� + { + pOid = (uint32_t*)(pTreatyBuff+16); + i=0; + while( DataLen >6 ) + { +// printf("\r\n--Cycle--%4x----.\r\n",ntohl(*pOid)); //����ʹ�� + pChar = (uint8_t*)pOid; + switch(ntohl(*pOid)) + { + + case SYSTERM_TIME: //ϵͳʱ�� + { + DeviceConfig.Time_Year =*(pChar+6); + DeviceConfig.Time_Mon =*(pChar+7); + DeviceConfig.Time_Mday =*(pChar+8); + DeviceConfig.Time_Hour =*(pChar+9); + DeviceConfig.Time_Min =*(pChar+10); + DeviceConfig.Time_Sec =*(pChar+11); + printf("\r\n-��-��-��-ʱ-��-�룺-%d--%d--%d--%d--%d--%d--.\r\n",DeviceConfig.Time_Year,DeviceConfig.Time_Mon,DeviceConfig.Time_Mday, + DeviceConfig.Time_Hour,DeviceConfig.Time_Min, DeviceConfig.Time_Sec ); //����ʹ�� + if((DeviceConfig.Time_Mon<=12)&&(DeviceConfig.Time_Mday<=31)&&(DeviceConfig.Time_Hour<=23)&&(DeviceConfig.Time_Min<=60)&&(DeviceConfig.Time_Sec<=60)) //�����Ϸ����ж� + { +// RTC_Timing(2); //ͨ���������·���������RTCʱ��У׼ + } + ParaRequest.OID_List[i] =SYSTERM_TIME; + pOid =(uint32_t*)(pChar+12); //ָ�����1��Tag + DataLen = DataLen-12; + i++; //�Ϸ�OID������ + break; + } + case FRAM_STATE: + { + FramStatus= pChar[6]*256+pChar[7]; //��ȡ��λ�����ݽ���״̬ + ParaRequest.OID_List[i] =FRAM_STATE; + pOid =(uint32_t*)(pChar+8); //ָ�����1��Tag + DataLen = DataLen-8; + i++; //�Ϸ�OID������ + printf("\r\nServer Receive Startup Response Status��0x%.4x\r\n",FramStatus); //����ʹ�� + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!Tag OID not recognition!\r\n"); //����ʹ�� +#endif + pOid =(uint32_t*)(pChar+1); //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + DataLen = DataLen-1; //ָ�����һ���ֽڣ���ѯ�������޺Ϸ�OID + break; + } + } + } + } + else + { +#if DEBUG_TEST + printf("\r\nWarning!!Receive Startup Response Data Not Rrecognition!\r\n"); //����ʹ�� +#endif + } + + break; + } + default: + { +#if DEBUG_TEST + printf("\r\nWarning!!PDU Type not recognition!\r\n"); //����ʹ�� +#endif + break; + } + } +} + +int my_strlen(char *str) +{ +// assert(str != NULL); + if (*str == '\0') + { + return 0; + } + else + { + return (1 + my_strlen(++str)); + } +} + +/******************************************************************************* +* Function Name : XX +* Description : 433ģ��������ݽ�������һ�㣩 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint16_t Receive_Data_Analysis(char* recev_buff) +{ + uint16_t ReceiveFlag = 0; //����������ȷ�Ա�־���� + char* pRecevBuff = NULL; + char RecevFromCollector[2] = {0xA3, 0x20}; //�������ݼ��������ݱ�־����,�������汾���иĶ������������Ҫ����Ӧ�޸� + uint8_t PayloadLen = 0; + uint8_t CrcSize = 0; + uint16_t CrcVerify = 0x0000; + uint16_t CrcRecev = 0x0000; + uint8_t j = 0; + char TempBuffer[RECEIVEBUFF_SIZE]; + uint8_t count = 0; + + printf("\r\n�������ݽ���!!\r\n"); + if(SIMCard_Type == 1) /* ����ƽ̨���ַ�ת��hex��ʽ */ + { + pRecevBuff = Find_SpecialString(recev_buff, "A320", 1024, 4); //���ģ������״̬ + + if(pRecevBuff != NULL) + { + string_to_hex(TempBuffer, pRecevBuff); + + PayloadLen = TempBuffer[2] * 256 + TempBuffer[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n T PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + } + } + else + { + pRecevBuff = Find_SpecialString(recev_buff, RecevFromCollector, 1024, strlen(RecevFromCollector)); + if(pRecevBuff != NULL) + { + PayloadLen = pRecevBuff[2] * 256 + pRecevBuff[3] + 6 - 16 - 2; //Tag-list���ݳ��� + if((PayloadLen % 8) != 0) + { + PayloadLen += (8 - (PayloadLen % 8)); + } + PayloadLen += 18;//�����������ݳ��� + printf("\r\n MU PayloadLen:%d \r\n", PayloadLen); //����ʹ�� + for(j = 0; j < PayloadLen; j++) + { + TempBuffer[j] = pRecevBuff[j]; + } + } + } + + if((pRecevBuff != NULL) && (pRecevBuff < recev_buff + (1024 - 1 - 3))) //��ָֹ��Խ�� + { + CrcSize = PayloadLen - 2; //����У�����ݳ��ȣ�������CRC�ֶ� + CrcVerify = CRC16((uint8_t*)TempBuffer, CrcSize); + CrcRecev = TempBuffer[CrcSize] * 256 + TempBuffer[CrcSize + 1]; //��λ��CRC���ֽ���ǰ���ֽ��ں�����λ������һ�� + if(CrcRecev == CrcVerify) + { + printf("\r\nReceive data right!!\r\n"); //����ʹ�� + Tea_Decrypt((uint8_t*)(TempBuffer + 16), PayloadLen - 16 - 2); + Treaty_Data_Analysis((uint8_t*)TempBuffer, &ReceiveFlag); //������������ + } + else + { + printf("\r\nReceive data not correct!!\r\n"); //����ʹ�� + } + + while (1) + { + if (count > 10) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(500); + pRecevBuff = NULL; + } + else + { + pRecevBuff = NULL; + printf("\r\nReceive data invalid!\rConsult Finish!\r\n"); //����ʹ�� + } + + printf("\r\n�������ݽ������!!\r\n");//����ʹ�� + return ReceiveFlag; +} diff --git a/gprs.h b/gprs.h new file mode 100644 index 0000000..1569b6a --- /dev/null +++ b/gprs.h @@ -0,0 +1,13 @@ +#ifndef _GPRS_H_ +#define _GPRS_H_ +#include "AiderProtocol.h" + +//�������� +unsigned short CRC16( unsigned char * pMsg, unsigned short iSize ); +char* Find_SpecialString(char* Source, char* Object, short Length_Source, short Length_Object); +uint16_t Receive_Data_Analysis(char* recev_buff); //����ԭʼ���ݽ��� +void Treaty_Data_Analysis(uint8_t* pTreatyBuff, uint16_t* pFlag); //Э�����ݽ�����ָ����9�ֽ�֮������ݲ��ֽ��� +int string_to_hex(char* h,char s[]); + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..416f609 --- /dev/null +++ b/main.c @@ -0,0 +1,424 @@ +/******************************************************************************* + * @file main.c + * @author casic 203 + * @version V1.0 + * @date 2019-06-14 + * @brief + * @attention +*********************************************************************************/ + +#include "string.h" +#include "gprs.h" +#include "rtc.h" +#include "AiderProtocol.h" +#include "API.h" +#include "sleep.h" +#include "math.h" +#include "NB-IOT.h" +#include "flash.h" + +#include "gsdk_sdk.h" +#include +#include +#include "gsdk_api.h" + +struct DeviceSet DeviceConfig = {0x00}; //������Ϣ�ṹ�� +struct Config_RegPara ConfigData = {0x00}; //������·����ò�����HEX��ʽ��������ϵͳ��������ģʽ֮ǰд��BKP�Ĵ��� +struct BusinessData businessData; +struct DataFrame dataFrame; + +uint8_t DeviceID[] = DEVICE_ID; //��ʼ���豸ID�� +uint16_t NodeAddr =0x0000; +uint8_t RouteControl =0x03; //·�ɿ��Ʊ�����Ĭ��ʹ�����ݼ������������ݴ��� +uint8_t send_buff[300]= {'\0'}; +uint8_t history_send_buff[300]= {'\0'}; +double GASData=200; +uint16_t ACR; + +static gsdk_handle_t g_huart; +char TestReceiveBuff[300]; + +gsdk_handle_t hgpio; + +double Gas_DataGet(void); +extern uint8_t SIMCard_Type; + +uint8_t PD2_Flag = 0;//��ñ��־��1����203��ƽ̨������ + +int __io_puts(char *data, int len) +{ + int ret = 0; + if (g_huart) + { + ret = gsdk_uart_write(g_huart, (uint8_t *)data, len, 1000); + } + return ret; +} + +int log_init(void) +{ + uart_config_t uart_config; + gsdk_status_t status; + + uart_config.baudrate = UART_BAUDRATE_115200; + uart_config.parity = UART_PARITY_NONE; + uart_config.stop_bit = UART_STOP_BIT_1; + uart_config.word_length = UART_WORD_LENGTH_8; + + status = gsdk_uart_open(UART_0, &uart_config, &g_huart); + if (status != GSDK_SUCCESS) + { + gsdk_syslog_printf("[GPIO_DEMO]: failed to open uart %d\n", status); + return -1; + } + return 0; +} + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t Gpio_Init(void) +{ + gsdk_status_t status; + gpio_config_t gpio_config; + gpio_pin_t gpio_pin; + + gpio_pin = GPIO_PIN_9; + + gpio_config.direction = GPIO_DIRECTION_OUTPUT; + gpio_config.pull = GPIO_PULL_NONE; + gpio_config.int_mode = GPIO_INT_DISABLE; + gpio_config.debounce_time = 0; + gpio_config.callback = NULL; + + status = gsdk_gpio_open(gpio_pin, &gpio_config, &hgpio); + if (status != GSDK_SUCCESS) + { + goto _fail; + } + + return 0; + +_fail: + gsdk_gpio_close(hgpio); + printf("[GPIO_DEMO] Gpio test failed!\n"); + return -1; +} + +void gpio_callback(void *user_data) +{ + printf("[GPIO_DEMO] gpio eint handle callback!\n"); +} + +/******************************************************************************* +* Function Name : void PeripheralInit(void) +* Description : ��ʼ���˿ڼ����� +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +uint8_t PeripheralInit(void) +{ + uint8_t Boot_Mode_Flag = 0; + uint8_t count = 0; + uint8_t RunCount = 0; + + Gpio_Init(); + log_init(); + gsdk_gpio_write(hgpio, GPIO_DATA_HIGH);//��ȼ��̽ͷ��Դ + Boot_Mode_Flag = Get_Boot_Mode(); + bsp_Init_DS2782(); + gsdk_ril_init(); + + Module_Flash_Read(RUN_COUNT_ADDR, &RunCount, 1); //��Flash�ж�ȡ�ɼ�Һλ���� + if (RunCount > 24) //�豸�����ⲿ��λ + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + else if (RunCount == 24) + { + RunCount = 0; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + RunCount++; + Module_Flash_Write(RUN_COUNT_ADDR - FLASH_TEST_BASE, &RunCount, 1); + } + printf("\n\r RunCount:%d \r\n", RunCount); + + gsdk_ril_set_ip_mode(IPV4); + gsdk_ril_set_cfun_mode(1); // ����ģ��ĵ绰����Ϊ1 - ȫ���� + while (1) + { + if (count > 6) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(300); + RTC_Init(); //��ȡϵͳ��ǰʱ�� + + printf("\n\r****************************************************************\r\n"); + printf("\n\r*-----��ӭʹ��BIRMM-GT200N ȼ�����ܼ��ܶ�!---------------*\r\n"); + printf("\n\r*-----����汾�ţ�%s ------------------------*\r\n", SOFTWARE_VERSION); + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + printf("\n\r*-----��Ȩ���У��������ߵ���������о���------------------*\r\n"); + printf("\n\r****************************************************************\r\n"); + + return Boot_Mode_Flag; +} + +/******************************************************************************* +* Function Name : int main(void) +* Description : ������ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +int main(void) +{ + uint16_t Boot_Mode_Flag, StartupResponse_Flag; + + Boot_Mode_Flag = PeripheralInit(); //��ʼ������ + ConfigData_Init(); //��FLASH��ȡ���ò��� + +/* Module_DS2781_Set_Register(); + vTaskDelay(100); + + while(1) + { + printf("\n\r*-----�豸��ţ�%.2x-%.2x-%.2x%.2x-%.2x%.2x---------------------------*\r\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5]); + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + printf("\r\n Get_DS2782_RAAC() is : %d \r\n", Get_DS2782_RAAC()); + printf("\r\n Get_DS2782_RSAC() is : %d \r\n", Get_DS2782_RSAC()); + printf("\r\n Get_DS2782_RSRC() is : %d \r\n", Get_DS2782_RSRC()); + + vTaskDelay(100); + }*/ + + //�����⵽���磬�ϴ����� + if(Boot_Mode_Flag == 2) + { +// RTC_Timing(1); + PD2_Flag = 1; + Module_DS2781_Set_Register(); + businessData.HistoryDateCount = 0; + Module_Flash_Write(HistoryDateCount_ADDR - FLASH_TEST_BASE, &(businessData.HistoryDateCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + Code_Read_NB(); + StartupResponse_Flag = StartupRequest(send_buff,8);//������֡�ϴ� + if(StartupResponse_Flag==0x099C) printf("\r\n IMEI ICCID �ϱ��ɹ�����\r\n"); + else printf("\r\n IMEI ICCID �ϱ�ʧ�ܣ���\r\n"); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else if(Boot_Mode_Flag == 3) + { + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� + } + + if(businessData.SendCount>0) //������ʷ���� + { + Code_Read_NB(); + + if(SIMCard_Type == 1) //����NB�����͵�IOTƽ̨ + { + SendMessage_NB_T(history_send_buff, businessData.HistoryDateCount); + } + else if(SIMCard_Type == 0) //�ƶ�����ͨNB��ֱ�ӷ��͵���̨������ + { + SendMessage_NB_MU(history_send_buff, businessData.HistoryDateCount); + } + else + { + printf("\r\n ===========SIM���쳣���޷�������===========!!\r\n"); + } + businessData.DataCount = 0; + Module_Flash_Write(CollectedDateCount_ADDR - FLASH_TEST_BASE, &(businessData.DataCount), 1); + businessData.SendCount = 0; + Module_Flash_Write(SendCount_ADDR - FLASH_TEST_BASE, &(businessData.SendCount), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + if(businessData.ParameterReply>0) //������ʷ���� + { + Code_Read_NB(); + StartupRequest(send_buff,12);//�������ûظ��ϴ� + businessData.ParameterReply = 0; + Module_Flash_Write(ParameterReply_ADDR - FLASH_TEST_BASE, &(businessData.ParameterReply), 1); + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + printf("\r\n Get_DS2782_Temperature() is : %d \r\n", Get_DS2782_Temperature()); + printf("\r\n Get_DS2782_RARC() is : %d \r\n", Get_DS2782_RARC()); + printf("\r\n Get_DS2782_Current() is : %d \r\n", Get_DS2782_Current()); + printf("\r\n Get_DS2782_Voltage() is : %d \r\n", Get_DS2782_Voltage()); + ACR = Get_DS2782_ACR(); //��ص��� + printf("\r\n Get_DS2782_ACR() is : %d \r\n", ACR); + + DeviceConfig.CollectPeriod = 1; + + /*����Ѳɼ����ݴ��ڲɼ�����*/ + if((businessData.DataCount >= DeviceConfig.CollectNum)) + { + Code_Read_NB(); + StartupResponse_Flag = TrapRequest(send_buff,4);//������֡�ϴ� + gsdk_ril_set_hardware_reboot();//����ģ�� + } + else + { + BusinessData_Collection(); + if(GASData <=100) + { + BusinessData_Storage(); + } + else{ + Code_Read_NB(); + TrapRequest(send_buff,4);//������֡�ϴ� + } + gsdk_ril_set_hardware_reboot();//����ģ�� + } + + gotoSleep(DeviceConfig.CollectPeriod);//����һ���ɼ����� +} + +double Gas_DataGet(void) +{ + uint16_t TestReceiveLength = 0; + char *pRecevBuff = NULL; + char *pGasRecevBuff = NULL; + char RecevFromGas[4] = {0x20,0x30,0x30,0x20}; + char RecevFromRadar[2] = {0x0d,0x0a}; + uint8_t j = 0; + uint8_t pp,i,m; + char h1; + char s1[2]; + GASData=250; + uint8_t count = 0; + + printf("\r\n Ԥ��8��... \r\n"); + while (1) + { + if (count > 32) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(800);//Ԥ��8�� + + printf("\r\n Ԥ�Ƚ���... \r\n"); + for(j = 0; j < 30; j++) + { + printf("\r\n ��ʼ����... \r\n"); + TestReceiveLength = gsdk_uart_read(g_huart, (uint8_t *)TestReceiveBuff, 300, 1000); + printf("\r\n TestReceiveLength is : %d \r\n", TestReceiveLength); + if(TestReceiveLength > 0) + { + pGasRecevBuff = Find_SpecialString(TestReceiveBuff+180, RecevFromGas, strlen(TestReceiveBuff), 4); + if(pGasRecevBuff != NULL) //��ָֹ��Խ�� + { + printf("\r\n pGasRecevBuff is : %s \r\n", pGasRecevBuff); + pRecevBuff = Find_SpecialString(pGasRecevBuff, RecevFromRadar, strlen(pGasRecevBuff), 2); + for(m = 0; m < 29; m++) + { + printf("\r\npRecevBuff---------- %.2x\r\n", pRecevBuff[m]); + } + if(pRecevBuff != NULL) //��ָֹ��Խ�� + { + if((pRecevBuff[9]==0x20)&&(pRecevBuff[15]==0x20)&&(pRecevBuff[23]==0x20)&&(pRecevBuff[26]==0x20)) + { + //printf("\r\nUART4�������ݽ���!!\r\n"); + pp= pRecevBuff[2]; + for(i=1; i<25; i++) + { + pp^= pRecevBuff[i+2]; + } + s1[0]= pRecevBuff[27]; + s1[1]= pRecevBuff[28]; + string_to_hex(&h1,s1); + + printf("\r\nUART4 У�� %.2x %.2x!!\r\n",pp,h1); + if(pp== h1) //��λ���У�� + { + if(pRecevBuff[24]==0x30) //�����ź� + { + printf("\r\npRecevBuff[25]---------------------------- %.2x!!\r\n",pRecevBuff[25]); + if(pRecevBuff[25]==0x30) //�����ź� + { + printf("\r\n ����״̬���� \r\n"); + if( pRecevBuff[6]==0x2e ) //С���� + { + GASData =(pRecevBuff[3]-0x30)*100 + (pRecevBuff[4]-0x30)*10 + (pRecevBuff[5]-0x30)*1.0 + (pRecevBuff[7]-0x30)*0.1 + (pRecevBuff[8]-0x30)*0.01 ; + if(pRecevBuff[2]==0x2b ) // ������ + { + + } + else if(pRecevBuff[2]==0x2d) // ������ + { + GASData=-GASData; + } + else + { + GASData =250;//ȼ��������ͨ���쳣 + } + } + } + else if(pRecevBuff[25]==0x31) + { + printf("\r\n ��·�쳣���޷�����ʹ�� \r\n"); + GASData =240; + } + else if(pRecevBuff[25]==0x32) // + { + printf("\r\n �¶�ѹ���쳣 \r\n"); + GASData =240; + } + } + else + { + printf("\r\n ȼ��̽ͷ�쳣 \r\n"); + } + } + } + } + } + } + if(GASData <=100) break; + while (1) + { + if (count > 2) + { + count = 0; + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(100); + memset(TestReceiveBuff, 0x00, 300); + } + printf("\r\n GASData is : %d \r\n", (uint8_t)GASData); + + gsdk_gpio_write(hgpio, GPIO_DATA_LOW);//�ر�ȼ��̽ͷ��Դ + return GASData; +} \ No newline at end of file diff --git a/rtc.c b/rtc.c new file mode 100644 index 0000000..450f4c7 --- /dev/null +++ b/rtc.c @@ -0,0 +1,340 @@ + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" +#include "rtc.h" +#include "AiderProtocol.h" + +#define DEBUG_LOG(fmt,...) printf("[LOGD]:F:%s,L:%d,"fmt,__func__,__LINE__,##__VA_ARGS__); +#define WARN_LOG(fmt,...) printf("[LOGW]:"fmt,##__VA_ARGS__); + +extern struct DeviceSet DeviceConfig; + +static gsdk_handle_t rtc_handle; + +void rtc_alarm_cb(void *user_data) +{ + (void)user_data; + gsdk_ril_init(); + gsdk_ril_set_hardware_reboot();//����ģ�� + OEM_LOGI("[RTC_DEMO] rtc alarm handle cb------------------------0!\n"); +} + +void utc_to_bj_time(rtc_time_t rtc_time) +{ + int year,month,day,hour; + int lastday = 0; //last day of this month + int lastlastday = 0; //last day of last month + char temp_buf[100] = {0}; + + year = rtc_time.rtc_year + 2000; //utc time + month = rtc_time.rtc_mon; + day = rtc_time.rtc_day; + hour = rtc_time.rtc_hour + 8; + + if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) + { + lastday = 31; + lastlastday = 30;//����Ӧ������ϸ��µ����� + + if(month == 3) + { + if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year + lastlastday = 29; + else + lastlastday = 28; + } + + if(month == 8 || month == 1)//����Ӧ����8�º�1�£���Ϊ8�º�1�µ���һ���µ�������31��� + lastlastday = 31; + } + else if(month == 4 || month == 6 || month == 9 || month == 11) + { + lastday = 30; + lastlastday = 31; + } + else + { + lastlastday = 31; + + if((year%400 == 0)||(year%4 == 0 && year%100 != 0)) + lastday = 29; + else + lastday = 28; + } + + if(hour >= 24) // if >24, day+1 + { + hour -= 24; + day += 1; + + if(day > lastday) // next month, day-lastday of this month + { + day -= lastday; + month += 1; + + if(month > 12) // next year , month-12 + { + month -= 12; + year += 1; + } + } + } + if(hour < 0) // if <0, day-1 + { + hour += 24; + day -= 1; + if(day < 1) // month-1, day=last day of last month + { + day = lastlastday; + month -= 1; + if(month < 1) // last year , month=12 + { + month = 12; + year -= 1; + } + } + } + + sprintf(temp_buf, + "Beijing time,%4d,%02d,%02d,%02d,%02d,%02d", + year, + month, + day, + hour, + rtc_time.rtc_min, + rtc_time.rtc_sec); + + OEM_LOGI("��ǰʱ��: %s!\n", temp_buf); + + DeviceConfig.Time_Year = year-2000;/* ϵͳ���ڣ�OID��0x10000050 */ + DeviceConfig.Time_Mon = month; + DeviceConfig.Time_Mday = day; + DeviceConfig.Time_Hour = hour; + DeviceConfig.Time_Min = rtc_time.rtc_min; +} + +/******************************************************************************* +* Function Name : RTC_Init +* Description : RTC��ʼ������ȡϵͳʱ�� +* Input : struct DeviceSet* DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Init(void) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + status = gsdk_rtc_open(&rtc_handle); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_open failed!\n"); + goto _fail; + } + + status = gsdk_rtc_time_read(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE("[RTC_DEMO] gsdk_rtc_time_read failed!\n"); + goto _fail; + } + + utc_to_bj_time(rtc_time); + + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Init failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : RTC_Timing +* Description : RTCУʱ +* Input : uint8_t SetTime_Flag��1ʱ��ָ��������ã�2ʱ������Ϊ������Уʱʱ�䣩, struct DeviceSet DeviceConfig +* Output : none +* Return : 1�ɹ���-1ʧ�� +*******************************************************************************/ +int RTC_Timing(uint8_t SetTime_Flag) +{ + rtc_time_t rtc_time = {0}; + gsdk_status_t status; + + if(SetTime_Flag == 1) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = 21; + rtc_time.rtc_mon = 1; + rtc_time.rtc_day = 1; + rtc_time.rtc_week = 1; + rtc_time.rtc_hour = 1; + rtc_time.rtc_min = 1; + rtc_time.rtc_sec = 1; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + else if(SetTime_Flag == 2) + { + OEM_LOGI("UE will adjust rtc time\r\n"); + rtc_time.rtc_year = DeviceConfig.Time_Year; + rtc_time.rtc_mon = DeviceConfig.Time_Mon; + rtc_time.rtc_day = DeviceConfig.Time_Mday; + rtc_time.rtc_hour = DeviceConfig.Time_Hour; + rtc_time.rtc_min = DeviceConfig.Time_Min; + rtc_time.rtc_sec = DeviceConfig.Time_Sec; + + status = gsdk_rtc_time_write(rtc_handle, &rtc_time); + if (status != GSDK_SUCCESS) + { + OEM_LOGE(" adjust rtc time failed %d!\n", status); + goto _fail; + } + } + return 1; + +_fail: + OEM_LOGI("[RTC_DEMO] RTC_Timing failed!\n"); + gsdk_rtc_close(rtc_handle); + return -1; +} + +/******************************************************************************* +* Function Name : Set_Alarm +* Description : RTC�������� +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : 1�ɹ���0ʧ�� +*******************************************************************************/ +void Set_Alarm(long SleepPara) +{ + gsdk_status_t status; + int i,j; + gsdk_handle_t g_alarm_handle; + uint8_t count = 0; + + if(SleepPara > 0) + { + /*configure alarm timer property*/ + gsdk_alarm_config_t alarm_config; + alarm_config.callback = rtc_alarm_cb;//timer callback + alarm_config.interval = SleepPara;//��λ��s + alarm_config.periodic = 1;//timer perform periodically + status = gsdk_alarm_open(&g_alarm_handle, &alarm_config);//open alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("open alarm failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("open alarm success\r\n"); + status = gsdk_alarm_start(g_alarm_handle);//start up alarm timer + if (status != GSDK_SUCCESS) + { + DEBUG_LOG("gsdk_alarm_start failed :%d\r\n", status); + goto _fail; + } + WARN_LOG("start alarm success\r\n"); + } + gsdk_rtc_close(rtc_handle); + + for(i=0; i<3; i++) + { + status = gsdk_ril_psm_set(CPSMS, "AT+CPSMS=1");//set "CPSMS=1" is power saving mode(PSM) + if(GSDK_ERROR == status) + { + DEBUG_LOG("set CPSMS=1 cmd error\r\n"); + goto _fail; + } + WARN_LOG("set CPSM=1 cmd success\r\n"); + + status = gsdk_ril_psm_set(ZSLR, "AT+ZSLR");//sleep command + if(GSDK_ERROR == status) + { + DEBUG_LOG("set ZSLR cmd error\r\n"); + goto _fail; + } + + WARN_LOG("wait for sleep....\r\n"); + for(j=0; j<3; j++) + { + DEBUG_LOG("-->ME3616 IS SLEEP <--\r\n"); + while (1) + { + if (count > 20) + { + break; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + count++; + } +// vTaskDelay(1000); //��ʱ10S���ȴ�˯�� + DEBUG_LOG("-->ME3616 IS WAKE <--\r\n"); + } + } + + gsdk_ril_set_hardware_reboot(); + +_fail: + WARN_LOG("Set_Alarm fail....\r\n"); + gsdk_ril_reset_module(); +} + +/******************************************************************************* +* Function Name : Get_Boot_Mode +* Description : ��ȡNBģ������ģʽ +* Input : long SleepPara������ʱ�䣬Ϊ0ʱ����������ֱ������ +* Output : none +* Return : Boot_Mode_Flag +*******************************************************************************/ +uint8_t Get_Boot_Mode(void) +{ + gsdk_boot_mode_t boot_mode; + gsdk_rtc_wakeup_mode_t wakeup_mode; + uint8_t Boot_Mode_Flag = 0; + + /*get boot mode function ,because this demo maybe enter PSM */ + boot_mode = gsdk_sys_get_boot_mode(&wakeup_mode); + DEBUG_LOG("boot_mode is---------------------------------------------- :%d\r\n", boot_mode); + if (1 == boot_mode || 2 == boot_mode) + { +// printf("wakeup from deep sleep\r\n"); + switch (wakeup_mode) + { + case GSDK_BOOT_RTC_TC_WAKEUP: + WARN_LOG("it's awakened by RTC_TC\r\n"); + break; + case GSDK_BOOT_RTC_EINT_WAKEUP: + WARN_LOG("it's awakened by RTC_EINT\r\n"); + break; + case GSDK_BOOT_RTC_ALARM_WAKEUP: + WARN_LOG("it's awakened by ALARM_WAKEUP\r\n"); + Boot_Mode_Flag = 1; + break; + case GSDK_BOOT_POWERKEY_WAKEUP: + WARN_LOG("it's awakened by POWERKEY\r\n"); + break; + default: + WARN_LOG("unknown mode\r\n"); + break; + } + } + else if (5 == boot_mode || 6 == boot_mode) + { + Boot_Mode_Flag = 3; + WARN_LOG("GSDK_BOOT_WDT_SW_RESET\r\n"); + } + else + { + Boot_Mode_Flag = 2; + WARN_LOG("not enter deep sleep\r\n"); + } + return Boot_Mode_Flag; +} \ No newline at end of file diff --git a/rtc.h b/rtc.h new file mode 100644 index 0000000..63f5cdb --- /dev/null +++ b/rtc.h @@ -0,0 +1,18 @@ +#ifndef __RTC_H +#define __RTC_H + +#include +#include +#include +#include "gsdk_sdk.h" +#include "gsdk_ril.h" + +int RTC_Init(void); //��ʼ��RTC +int RTC_Timing(uint8_t SetTime_Flag); +void Set_Alarm(long SleepPara); +uint8_t Get_Boot_Mode(void); +void utc_to_bj_time(rtc_time_t rtc_time); + +#endif + + diff --git a/sleep.c b/sleep.c new file mode 100644 index 0000000..a1e64e6 --- /dev/null +++ b/sleep.c @@ -0,0 +1,26 @@ + +#include "string.h" +#include "rtc.h" +#include "sleep.h" +#include "gsdk_api.h" +#include "gsdk_sdk.h" +#include +#include +#include + +/******************************************************************************* +* Function Name : XX +* Description : XX +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void gotoSleep(long SleepPara) +{ + gsdk_ril_set_cfun_mode(0); // ����ģ��ĵ绰����Ϊ0 - ��С���ܣ���ʱ����SIM��Ҳ�������� + + printf("\r\nSLEEP OK! %ld min later wakeup!!\r\n", SleepPara); + Set_Alarm(SleepPara*60); +} + + diff --git a/sleep.h b/sleep.h new file mode 100644 index 0000000..d18c287 --- /dev/null +++ b/sleep.h @@ -0,0 +1,7 @@ +#ifndef _SLEEP_H_ +#define _SLEEP_H_ + +void gotoSleep(long SleepPara); + +#endif + diff --git a/~WRL0003.tmp b/~WRL0003.tmp new file mode 100644 index 0000000..a37dbb4 --- /dev/null +++ b/~WRL0003.tmp Binary files differ diff --git "a/\344\277\256\346\224\271\350\256\260\345\275\225.doc" "b/\344\277\256\346\224\271\350\256\260\345\275\225.doc" new file mode 100644 index 0000000..2497e5b --- /dev/null +++ "b/\344\277\256\346\224\271\350\256\260\345\275\225.doc" Binary files differ