电子元器件论坛
直播中

kiki

9年用户 1604经验值
擅长:存储技术 存储技术 存储技术
私信 关注

【Io开发笔记】机智云智能浇花器实战(3)-自动生成代码移植

第一篇内容:总体设计/系统功能介绍/机智云自助开发平台-开发利器GAgent等等

点击下载:【Io开发笔记】机智云智能浇花器实战(1)-基础Demo实现

第二篇内容:

继电器实现/功能测试/DHT11驱动代码实现/OLED屏幕显示传感器数据/中文字模制作等等

点击下载:机智云智能浇花器实战(2)-基础Demo实现

一,BH1750光照传感器原理图

二,BH1750传感器代码

  1. #include "bh1750.h"
  2. #include "delay.h"
  3. uint8_t BUF[8]; //接收数据缓存区
  4. int mcy; //进位标志
  5. / 开始信号 /
  6. void BH1750_Start()
  7. {
  8. BH1750_SDA_H; //拉高数据线
  9. BH1750_SCL_H; //拉高时钟线
  10. Delay_nus(5); //延时
  11. GPIO_ResetBits(BH1750_PORT, BH1750_SDA_PIN); //产生下降沿
  12. Delay_nus(5); //延时
  13. GPIO_ResetBits(BH1750_PORT, BH1750_SCL_PIN); //拉低时钟线
  14. }
  15. / 停止信号 /
  16. void BH1750_Stop()
  17. {
  18. 
    

BH1750_SDA_L;                                             //拉低数据线

BH1750_SCL_H; //拉高时钟线


Delay_nus(5);                                       //延时

GPIO_SetBits(BH1750_PORT, BH1750_SDA_PIN); //产生上升沿


Delay_nus(5);                 //延时

  1. }
  2. /******************
  3. 发送应答信号
  4. 入口参数:ack (0:ACK 1:NAK)
  5. ******************/
  6. void BH1750_SendACK(int ack)
  7. {

GPIO_InitTypeDef GPIO_InitStruct;


35. 
36. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
37. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
38. GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
39. GPIO_Init(BH1750_PORT, &GPIO_InitStruct);
40. 
41. ```
    
    ```

if(ack == 1)   //写应答信号

BH1750_SDA_H;


else if(ack == 0)

BH1750_SDA_L;


else

return;


47. BH1750_SCL_H;     //拉高时钟线
48. Delay_nus(5);                 //延时
49. BH1750_SCL_L;      //拉低时钟线
50. Delay_nus(5);                //延时
51. }
52. 
53. /******************
54. 接收应答信号
55. ******************/
56. int BH1750_RecvACK()
57. {
58. GPIO_InitTypeDef GPIO_InitStruct;
59. 
60. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;  / *这里一定要设成输入上拉,否则不能读出数据* /
61. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
62. GPIO_InitStruct.GPIO_Pin=BH1750_SDA_PIN;
63. GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
64. 
65. BH1750_SCL_H;            //拉高时钟线
66. Delay_nus(5);                 //延时
67. ```
    
    ```

if(GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN)==1)//读应答信号

mcy = 1 ;


69. else
70. ```
    
    ```

mcy = 0 ;

  1. BH1750_SCL_L; //拉低时钟线
  2. Delay_nus(5); //延时
  3. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
  4. GPIO_Init(BH1750_PORT,&GPIO_InitStruct);

return mcy;


76. }
77. 
78. /******************
79. 向IIC总线发送一个字节数据
80. ******************/
81. void BH1750_SendByte(uint8_t dat)
82. {
83. uint8_t i;
84. for (i=0; i<8; i++)         //8位计数器
85. {
86. ```
    
    ```

if( 0X80 & dat )

GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN);


else

GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN);


dat <<= 1;

BH1750_SCL_H; //拉高时钟线


Delay_nus(5);             //延时

BH1750_SCL_L; //拉低时钟线


Delay_nus(5);            //延时

  1. }
  2. BH1750_RecvACK();
  3. }
  4. uint8_t BH1750_RecvByte()
  5. {
  6. uint8_t i;
  7. uint8_t dat = 0;
  8. uint8_t bit;
  9. GPIO_InitTypeDef GPIO_InitStruct;
  10. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; /这里一定要设成输入上拉,否则不能读出数据/
  11. GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
  12. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_Init(BH1750_PORT,&GPIO_InitStruct );
  14. GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN); //使能内部上拉,准备读取数据,
  15. for (i=0; i<8; i++) //8位计数器
  16. {

dat <<= 1;


BH1750_SCL_H;               //拉高时钟线

Delay_nus(5); //延时


if( SET == GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN))

bit = 0X01;


else

bit = 0x00;


dat |= bit;             //读数据

BH1750_SCL_L; //拉低时钟线


Delay_nus(5);            //延时

  1. }

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;


128. GPIO_Init(BH1750_PORT, &GPIO_InitStruct );
129. return dat;
130. }
131. 
132. void Single_Write_BH1750(uint8_t REG_Address)
133. {
134. BH1750_Start();                  //起始信号
135. BH1750_SendByte(SlaveAddress);   //发送设备地址+写信号
136. BH1750_SendByte(REG_Address);    //内部寄存器地址,请参考中文pdf22页
137. //  BH1750_SendByte(REG_data);       //内部寄存器数据,请参考中文pdf22页
138. BH1750_Stop();                   //发送停止信号
139. }
140. 
141. //初始化BH1750,根据需要请参考pdf进行修改**
142. void BH1750_Init()
143. {
144. GPIO_InitTypeDef GPIO_InitStruct;
145. ```
     
     ```

/ *开启GPIOB的外设时钟* /

  1. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
  2. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  3. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  4. GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN | BH1750_SCL_PIN ;
  5. GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
  6. Single_Write_BH1750(0x01);
  7. Delay_nms(180); //延时180ms
  8. }
  9. //连续读出BH1750内部数据
  10. void mread(void)
  11. {
  12. uint8_t i;
  13. BH1750_Start(); //起始信号
  14. BH1750_SendByte(SlaveAddress+1); //发送设备地址+读信号

for (i=0; i<3; i++) //连续读取6个地址数据,存储中BUF


164. {
165. ```
     
     ```

BUF[i] = BH1750_RecvByte();          //BUF[0]存储0x32地址中的数据

if (i == 3)


{

BH1750_SendACK(1); //最后一个数据需要回NOACK


}

else


{

BH1750_SendACK(0); //回应ACK


}

  1. }
  2. BH1750_Stop(); //停止信号
  3. Delay_nms(5);
  4. }
  5. floatRead_BH1750(void)
  6. {
  7. int dis_data; //变量

float temp1;


float temp2;

Single_Write_BH1750(0x01); // power on


Single_Write_BH1750(0x10);   // H- resolution mode

Delay_nms(180); //延时180ms


mread();                     //连续读出数据,存储在BUF中

dis_data=BUF[0];


dis_data=(dis_data<<8)+BUF[1]; //合成数据

temp1=dis_data/1.2;


temp2=10*dis_data/1.2;

temp2=(int)temp2%10;


return temp1;

  1. }

复制代码

  1. #ifndef BH1750_H
  2. #define BH1750_H
  3. #include "STM32f10x.h"
  4. /*

定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改


8. ```
   ALT  ADDRESS引脚接地时地址为0x46,   接电源时地址为0xB8
  1. */
  2. #define SlaveAddress 0x46
  3. #define BH1750_PORT GPIOB
  4. #define BH1750_SCL_PIN GPIO_Pin_1
  5. #define BH1750_SDA_PIN GPIO_Pin_0
  6. #define BH1750_SCL_H GPIO_SetBits(BH1750_PORT,BH1750_SCL_PIN)
  7. #define BH1750_SCL_L GPIO_ResetBits(BH1750_PORT,BH1750_SCL_PIN)
  8. #define BH1750_SDA_H GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN)
  9. #define BH1750_SDA_L GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN)
  10. extern uint8_t BUF[8]; //接收数据缓存区
  11. extern int dis_data; //变量
  12. extern int mcy; //表示进位标志位
  13. void BH1750_Init(void);
  14. void conversion(uint32_t temp_data);
  15. void Single_Write_BH1750(uint8_t REG_Address); //单个写入数据
  16. uint8_t Single_Read_BH1750(uint8_t REG_Address); //单个读取内部寄存器数据
  17. void mread(void); //连续的读取内部寄存器数据
  18. float Read_BH1750(void);
  19. #endif

复制代码

BH1750传感器代码说明

核心板单独测试程序在PB0PB1管脚是完全正常,不知道是不是核心板的PB2上接了什么暂时还未排查出来问题,如果你是用开发板或者是自己设计的项目板的话,那么程序是直接可以使用的程序依然按照PB0PB1保留。

三,机智云自助开发平台数据点创建

机智云官方网站:https://www.gizwits.com/

步骤1,创建产品

创建好后就会有基本信息

步骤2,填写机智云产品ProductKey

这两个信息比较重要最好是保存下来

  1. Product Key :9c8a5a8e38344fb4af14b6db0f5b1df7
  2. Product Secret :45c86d8c6a2a4b1dac7d68df54f6e4f0

复制代码

步骤3,定义自己的数据点

只读:就是只允许赋值数据传感器赋值给平台平台只能读取

可写:就是数据可以被修改继电器的开关状态平台可以修改

四,MCU开发

mcu开发注意事项平台选Common其实就是STM32F103x平台

1,生成代码包

2,下载自动生成的代码包

3,机智云Gizwits协议移植

这两个文件夹要添加到自己的工程

这是添加的文件夹以及文件的目录

4,修改gizwits_product.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "gizwits_product.h"
  4. #include "usart3.h"
  5. static uint32_t timerMsCount;
  6. uint8_t aRxBuffer;
  7. dataPoint_t currentDataPoint;
  8. uint8_t wifi_flag;
  9. //存放事件处理API接口函数
  10. int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *gizdata, uint32_t len)
  11. {
  12. uint8_t i = 0;
  13. dataPoint_t *dataPointPtr = (dataPoint_t *)gizdata;
  14. moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)gizdata;
  15. protocolTime_t *ptime = (protocolTime_t *)gizdata;
  16. #if MODULE_TYPE
  17. gprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;
  18. #else
  19. moduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;
  20. #endif
  21. if((NULL == info) || (NULL == gizdata))
  22. {
  23. 
    

return -1;

  1. }
  2. for(i=0; inum; i++)
  3. {

switch(info->event[i])


{

case EVENT_Relay_1:


currentDataPoint.valueRelay_1 = dataPointPtr->valueRelay_1;

GIZWITS_LOG("Evt: EVENT_Relay_1 %d
", currentDataPoint.valueRelay_1);


if(0x01 == currentDataPoint.valueRelay_1)

{


currentDataPoint.valueRelay_1 = 1;

}


else

{


currentDataPoint.valueRelay_1 = 0;

}


break;

case WIFI_SOFTAP:


break;

case WIFI_AIRLINK:


break;

case WIFI_STATION:


break;

case WIFI_CON_ROUTER:


break;

case WIFI_DISCON_ROUTER:


break;

case WIFI_CON_M2M:


wifi_flag = 1; //WiFi连接标志

break;


case WIFI_DISCON_M2M:

wifi_flag = 0; //WiFi断开标志


break;

case WIFI_RSSI:


GIZWITS_LOG("RSSI %d
", wifiData->rssi);

break;


case TRANSPARENT_DATA:

GIZWITS_LOG("TRANSPARENT_DATA
");


//user handle , Fetch data from [data] , size is [len]

break;


case WIFI_NTP:

GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d]
",ptime->year,ptime->month,ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);


break;

case MODULE_INFO:


GIZWITS_LOG("MODULE INFO ...
");

#if MODULE_TYPE


GIZWITS_LOG("GPRS MODULE ...
");

//Format By gprsInfo_t


#else

GIZWITS_LOG("WIF MODULE ...
");


//Format By moduleInfo_t

GIZWITS_LOG("moduleType : [%d]
",ptModuleInfo->moduleType);


#endif

break;


default:

break;


}

  1. }
  2. return 0;
  3. }
  4. void userHandle(void)
  5. {
  6. /*

currentDataPoint.valueTemp = ;//Add Sensor Data Collection


currentDataPoint.valueHumi = ;//Add Sensor Data Collection

currentDataPoint.valueLight_Intensity = ;//Add Sensor Data Collection


102. */
103. }
104. void userInit(void)
105. {
106. ```
     
     ```

memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));

currentDataPoint.valueRelay_1 = 0;


currentDataPoint.valueTemp            = 0;

currentDataPoint.valueHumi = 0;


currentDataPoint.valueLight_Intensity = 0;

  1. }
  2. void gizTimerMs(void)
  3. {

timerMsCount++;


116. }
117. 
118. uint32_t gizGetTimerCount(void)
119. {
120. ```
     
     ```

return timerMsCount;

  1. }
  2. void mcuRestart(void)
  3. {

__set_FAULTMASK(1);


NVIC_SystemReset();

  1. }
  2. void TIMER_IRQ_FUN(void)
  3. {
  4. gizTimerMs();
  5. }
  6. void UART_IRQ_FUN(void)
  7. {
  8. uint8_t value = 0;
  9. gizPutData(&value, 1);
  10. }
  11. int32_t uartWrite(uint8_t *buf, uint32_t len)
  12. {

uint32_t i = 0;


if(NULL == buf)

{


return -1;

}


for(i=0; i<len; i++)

{


USART_SendData(USART3,buf[i]);

while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //循环发送,直到发送完毕


if(i >=2 && buf[i] == 0xFF)

{


USART_SendData(USART3, 0x55);

while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //循环发送,直到发送完毕


}

}


return len;

  1. }

复制代码

5,修改

  1. #ifndef _GIZWITS_PRODUCT_H
  2. #define _GIZWITS_PRODUCT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include<stdint.h>
  7. //#include "stm32f1xx.h"
  8. #include"gizwits_protocol.h"
  9. /**
    • MCU software version
  10. */
  11. #define SOFTWARE_VERSION "03030000"
  12. /**
    • MCU hardware version
  13. */
  14. #define HARDWARE_VERSION "03010100"
  15. /**
    • Communication module model
  16. */
  17. #define MODULE_TYPE 0 //0,WIFI ;1,GPRS
  18. /**[url=home.php?mod=space&uid=830701]@name[/url] TIM3 related macro definition
    • @{
  19. */
  20. #define TIMER TIM3
  21. #define TIMER_IRQ TIM3_IRQn
  22. #define TIMER_RCC RCC_APB1Periph_TIM3
  23. #define TIMER_IRQ_FUN TIM3_IRQHandler
  24. /**@} */
  25. /**@name USART related macro definition
    • @{
  26. */
  27. #define UART_BAUDRATE 9600
  28. #define UART_PORT 2
  29. #define UART USART2
  30. #define UART_IRQ USART2_IRQn
  31. #define UART_IRQ_FUN USART2_IRQHandler
  32. #if (UART_PORT == 1)
  33. #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
  34. #define UART_GPIO_CLK RCC_APB2Periph_GPIOA
  35. #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
  36. #define UART_AFIO_CLK RCC_APB2Periph_AFIO
  37. #define UART_CLK_Cmd RCC_APB2PeriphClockCmd
  38. #define UART_CLK RCC_APB2Periph_USART1
  39. #define UART_GPIO_PORT GPIOA
  40. #define UART_RxPin GPIO_Pin_10
  41. #define UART_TxPin GPIO_Pin_9
  42. #endif
  43. #if (UART_PORT == 2)
  44. #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
  45. #define UART_GPIO_CLK RCC_APB2Periph_GPIOA
  46. #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
  47. #define UART_AFIO_CLK RCC_APB2Periph_AFIO
  48. #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
  49. #define UART_CLK RCC_APB1Periph_USART2
  50. #define UART_GPIO_PORT GPIOA
  51. #define UART_RxPin GPIO_Pin_3
  52. #define UART_TxPin GPIO_Pin_2
  53. #endif
  54. #if (UART_PORT == 3)
  55. #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
  56. #define UART_GPIO_CLK RCC_APB2Periph_GPIOC
  57. #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
  58. #define UART_AFIO_CLK RCC_APB2Periph_AFIO
  59. #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
  60. #define UART_CLK RCC_APB1Periph_USART3
  61. #define UART_GPIO_PORT GPIOC
  62. #define UART_RxPin GPIO_Pin_11
  63. #define UART_TxPin GPIO_Pin_10
  64. #endif
  65. /**@} */
  66. /** User area the current device state structure*/
  67. extern dataPoint_t currentDataPoint;
  68. voidgizTimerMs(void);
  69. uint32_tgizGetTimerCount(void);
  70. voidtimerInit(void);
  71. voiduartInit(void);
  72. voiduserInit(void);
  73. voiduserHandle(void);
  74. voidmcuRestart(void);
  75. int32_tuartWrite(uint8_t *buf, uint32_t len);
  76. int8_tgizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len);
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif

复制代码

5,修改gizwits_product.h
Listitem
Listitem
Listitem
Listitem
Listitem

  1. /**

    • [url=home.php?mod=space&uid=1455510]@file[/url] gizwits_protocol.c
    • [url=home.php?mod=space&uid=2666770]@Brief[/url] Corresponding gizwits_product.c header file (including product hardware and software version definition)
    • [url=home.php?mod=space&uid=40524]@author[/url] Gizwits
    • @date 2017-07-19
    • [url=home.php?mod=space&uid=644434]@version[/url] V03030000
    • [url=home.php?mod=space&uid=855824]@copyright[/url] Gizwits
    • [url=home.php?mod=space&uid=1902110]@NOTE[/url] 机智云.只为智能硬件而生

Gizwits Smart Cloud for Smart Products


12. * ```
      
      ```

链接|增值ֵ|开放|中立|安全|自有|自由|生态

www.gizwits.com


14. * 
15. ***************************/
16. //#include "ringBuffer.h"
17. //#include "gizwits_product.h"
18. //#include "dataPointTools.h"
19. #include"delay.h"
20. /** Protocol global variables **/
21. //gizwitsProtocol_t gizwitsProtocol;
22. //extern dataPoint_t currentDataPoint;
23. //extern uint8_t wifi_flag;
24. 
25. /**@name The serial port receives the ring buffer implementation
26. * @{
27. */
28. rb_t pRb;                                               ///< Ring buffer structure variable
29. static uint8_t rbBuf[RB_MAX_LEN];                       ///< Ring buffer data cache buffer
30. 
31. 
32. /**@} */
33. 
34. /**
35. * @brief Write data to the ring buffer
36. * @param [in] buf        : buf adress
37. * @param [in] len        : byte length
38. * @return   correct : Returns the length of the written data
39. ```
    
    ```

failure : -1

  1. */
  2. int32_t gizPutData(uint8_t *buf, uint32_t len)
  3. {

int32_t count = 0;


44. 
45. ```
    
    ```

if(NULL == buf)

{


47. ```
    
    ```

GIZWITS_LOG("ERR: gizPutData buf is empty
");

return -1;


49. ```
    
    ```

}

count = rbWrite(&pRb, buf, len);


52. ```
    
    ```

if(count != len)

{


54. ```
    
    ```

GIZWITS_LOG("ERR: Failed to rbWrite
");

return -1;


56. ```
    
    ```

}

return count;


59. }
60. 
61. 
62. 
63. /**
64. * @brief Protocol header initialization
65. * 
66. * @param [out] head         : Protocol header pointer
67. * 
68. * @return 0, success; other, failure
69. */
70. static int8_t gizProtocolHeadInit(protocolHead_t *head)
71. {
72. ```
    
    ```

if(NULL == head)

{


74. ```
    
    ```

GIZWITS_LOG("ERR: gizProtocolHeadInit head is empty
");

return -1;


76. ```
    
    ```

}

memset((uint8_t *)head, 0, sizeof(protocolHead_t));


79. ```
    
    ```

head->head[0] = 0xFF;

head->head[1] = 0xFF;


81. 
82. ```
    
    ```

return 0;

  1. }
  2. /**
    • @brief Protocol ACK check processing function
    • @param [in] data : data adress
    • @param [in] len : data length
    • @return 0, suceess; other, failure
  3. */
  4. static int8_t gizProtocolWaitAck(uint8_t *gizdata, uint32_t len)
  5. {

if(NULL == gizdata)


96. ```
    
    ```

{

GIZWITS_LOG("ERR: data is empty
");


98. ```
    
    ```

return -1;

}


100. 
101. ```
     
     ```

memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));

memcpy((uint8_t *)gizwitsProtocol.waitAck.buf, gizdata, len);


103. ```
     
     ```

gizwitsProtocol.waitAck.dataLen = (uint16_t)len;

gizwitsProtocol.waitAck.flag = 1;


106. ```
     
     ```

gizwitsProtocol.waitAck.sendTime = gizGetTimerCount();

return 0;


109. }
110. /**
111. * @brief generates "controlled events" according to protocol
112. 
113. * @param [in] issuedData: Controlled data
114. * @param [out] info: event queue
115. * @param [out] dataPoints: data point data
116. * @return 0, the implementation of success, non-0, failed
117. */
118. static int8_t ICACHE_FLASH_ATTR gizDataPoint2Event(gizwitsIssued_t *issuedData, eventInfo_t *info, dataPoint_t *dataPoints)
119. {
120. ```
     
     ```

if((NULL == issuedData) || (NULL == info) ||(NULL == dataPoints))

{


122. ```
     
     ```

GIZWITS_LOG("gizDataPoint2Event Error , Illegal Param
");

return -1;


124. ```
     
     ```

}

/** Greater than 1 byte to do bit conversion **/


127. ```
     
     ```

if(sizeof(issuedData->attrFlags) > 1)

{


129. ```
     
     ```

if(-1 == gizByteOrderExchange((uint8_t *)&issuedData->attrFlags,sizeof(attrFlags_t)))

{


131. ```
     
     ```

GIZWITS_LOG("gizByteOrderExchange Error
");

return -1;


133. ```
     
     ```

}

}


135. 
136. ```
     
     ```

if(0x01 == issuedData->attrFlags.flagRelay_1)

{


138. ```
     
     ```

info->event[info->num] = EVENT_Relay_1;

info->num++;


140. ```
     
     ```

dataPoints->valueRelay_1 = gizStandardDecompressionValue(Relay_1_BYTEOFFSET,Relay_1_BITOFFSET,Relay_1_LEN,(uint8_t *)&issuedData->attrVals.wBitBuf,sizeof(issuedData->attrVals.wBitBuf));

}


142. 
143. 
144. ```
     
     ```

return 0;

  1. }
  2. /**
    • @brief contrasts the current data with the last data
    • @param [in] cur: current data point data
    • @param [in] last: last data point data
    • @return: 0, no change in data; 1, data changes
  3. */
  4. static int8_t ICACHE_FLASH_ATTR gizCheckReport(dataPoint_t *cur, dataPoint_t *last)
  5. {

int8_t ret = 0;


158. ```
     
     ```

static uint32_t lastReportTime = 0;

uint32_t currentTime = 0;


160. 
161. ```
     
     ```

if((NULL == cur) || (NULL == last))

{


163. ```
     
     ```

GIZWITS_LOG("gizCheckReport Error , Illegal Param
");

return -1;


165. ```
     
     ```

}

currentTime = gizGetTimerCount();


167. ```
     
     ```

if(last->valueRelay_1 != cur->valueRelay_1)

{


169. ```
     
     ```

GIZWITS_LOG("valueRelay_1 Changed
");

ret = 1;


171. ```
     
     ```

}

if(last->valueTemp != cur->valueTemp)


174. ```
     
     ```

{

if(currentTime - lastReportTime >= REPORT_TIME_MAX)


176. ```
     
     ```

{

GIZWITS_LOG("valueTemp Changed
");


178. ```
     
     ```

ret = 1;

}


180. ```
     
     ```

}

if(last->valueHumi != cur->valueHumi)


182. ```
     
     ```

{

if(currentTime - lastReportTime >= REPORT_TIME_MAX)


184. ```
     
     ```

{

GIZWITS_LOG("valueHumi Changed
");


186. ```
     
     ```

ret = 1;

}


188. ```
     
     ```

}

if(last->valueLight_Intensity != cur->valueLight_Intensity)


190. ```
     
     ```

{

if(currentTime - lastReportTime >= REPORT_TIME_MAX)


192. ```
     
     ```

{

GIZWITS_LOG("valueLight_Intensity Changed
");


194. ```
     
     ```

ret = 1;

}


196. ```
     
     ```

}

if(1 == ret)


199. ```
     
     ```

{

lastReportTime = gizGetTimerCount();


201. ```
     
     ```

}

return ret;


203. }
204. 
205. /**
206. * @brief User data point data is converted to wit the cloud to report data point data
207. * 
208. * @param [in] dataPoints: user data point data address
209. * @param [out] devStatusPtr: wit the cloud data point data address
210. * 
211. * @return 0, the correct return; -1, the error returned
212. */
213. static int8_t ICACHE_FLASH_ATTR gizDataPoints2ReportData(dataPoint_t *dataPoints , devStatus_t *devStatusPtr)
214. {
215. ```
     
     ```

if((NULL == dataPoints) || (NULL == devStatusPtr))

{


217. ```
     
     ```

GIZWITS_LOG("gizDataPoints2ReportData Error , Illegal Param
");

return -1;


219. ```
     
     ```

}

gizMemset((uint8_t *)devStatusPtr->wBitBuf,0,sizeof(devStatusPtr->wBitBuf));


222. 
223. ```
     
     ```

gizStandardCompressValue(Relay_1_BYTEOFFSET,Relay_1_BITOFFSET,Relay_1_LEN,(uint8_t *)devStatusPtr,dataPoints->valueRelay_1);

gizByteOrderExchange((uint8_t *)devStatusPtr->wBitBuf,sizeof(devStatusPtr->wBitBuf));


225. 
226. ```
     
     ```

devStatusPtr->valueTemp = gizY2X(Temp_RATIO,  Temp_ADDITION, dataPoints->valueTemp);

devStatusPtr->valueHumi = gizY2X(Humi_RATIO, Humi_ADDITION, dataPoints->valueHumi);


228. ```
     
     ```

devStatusPtr->valueLight_Intensity = gizY2X(Light_Intensity_RATIO,  Light_Intensity_ADDITION, dataPoints->valueLight_Intensity);

return 0;


234. }
235. 
236. 
237. /**
238. * @brief This function is called by the [GAgent](http://docs.gizwits.com/zh-cn/deviceDev/gagent_info.html) module to receive the relevant protocol data from the cloud or APP
239. * @param [in] inData The protocol data entered
240. * @param [in] inLen Enter the length of the data
241. * @param [out] outData The output of the protocol data
242. * @param [out] outLen The length of the output data
243. * @return 0, the implementation of success, non-0, failed
244. */
245. static int8_t gizProtocolIssuedProcess(char *did, uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t *outLen)
246. {
247. ```
     
     ```

uint8_t issuedAction = inData[0];

if((NULL == inData)||(NULL == outData)||(NULL == outLen))


250. ```
     
     ```

{

GIZWITS_LOG("gizProtocolIssuedProcess Error , Illegal Param
");


252. ```
     
     ```

return -1;

}


254. 
255. ```
     
     ```

if(NULL == did)

{


257. ```
     
     ```

memset((uint8_t *)&gizwitsProtocol.issuedProcessEvent, 0, sizeof(eventInfo_t));

switch(issuedAction)


259. ```
     
     ```

{

case ACTION_CONTROL_DEVICE:


261. ```
     
     ```

gizDataPoint2Event((gizwitsIssued_t *)&inData[1], &gizwitsProtocol.issuedProcessEvent,&gizwitsProtocol.gizCurrentDataPoint);

gizwitsProtocol.issuedFlag = ACTION_CONTROL_TYPE;


263. ```
     
     ```

outData = NULL;

*outLen = 0;


265. ```
     
     ```

break;

case ACTION_READ_DEV_STATUS:


268. ```
     
     ```

if(0 == gizDataPoints2ReportData(&gizwitsProtocol.gizLastDataPoint,&gizwitsProtocol.reportData.devStatus))

{


270. ```
     
     ```

memcpy(outData+1, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(gizwitsReport_t));

outData[0] = ACTION_READ_DEV_STATUS_ACK;


272. ```
     
     ```

*outLen = sizeof(gizwitsReport_t)+1;

}


274. ```
     
     ```

else

{


276. ```
     
     ```

return -1;

}


278. ```
     
     ```

break;

case ACTION_W2D_TRANSPARENT_DATA:


280. ```
     
     ```

memcpy(gizwitsProtocol.transparentBuff, &inData[1], inLen-1);

gizwitsProtocol.transparentLen = inLen - 1;


282. 
283. ```
     
     ```

gizwitsProtocol.issuedProcessEvent.event[gizwitsProtocol.issuedProcessEvent.num] = TRANSPARENT_DATA;

gizwitsProtocol.issuedProcessEvent.num++;


285. ```
     
     ```

gizwitsProtocol.issuedFlag = ACTION_W2D_TRANSPARENT_TYPE;

outData = NULL;


287. ```
     
     ```

*outLen = 0;

break;


289. 
290. ```
     
     ```

default:

break;


292. ```
     
     ```

}

}


294. 
295. ```
     
     ```

return 0;

  1. }
  2. /**
    • @brief The protocol sends data back , P0 ACK
    • @param [in] head : Protocol head pointer
    • @param [in] data : Payload data
    • @param [in] len : Payload data length
    • @param [in] proFlag : DID flag ,1 for Virtual sub device did ,0 for single product or gateway
    • @return : 0,Ack success;
  3. -1,Input Param Illegal
  4. -2,Serial send faild
  5. */
  6. static int32_t gizProtocolIssuedDataAck(protocolHead_t *head, uint8_t *gizdata, uint32_t len, uint8_t proFlag)
  7. {

int32_t ret = 0;


312. ```
     
     ```

uint8_t tx_buf[RB_MAX_LEN];

uint32_t offset = 0;


314. ```
     
     ```

uint8_t sDidLen = 0;

uint16_t data_len = 0;


316. ```
     
     ```

uint8_t *pTxBuf = tx_buf;

if(NULL == gizdata)


318. ```
     
     ```

{

GIZWITS_LOG("[ERR] data Is Null
");


320. ```
     
     ```

return -1;

}


322. 
323. 
324. ```
     
     ```

if(0x1 == proFlag)

{


326. ```
     
     ```

sDidLen = *((uint8_t *)head + sizeof(protocolHead_t));

data_len = 5 + 1 + sDidLen + len;


328. ```
     
     ```

}

else


330. ```
     
     ```

{

data_len = 5 + len;


332. ```
     
     ```

}

GIZWITS_LOG("len = %d , sDidLen = %d ,data_len = %d
", len,sDidLen,data_len);


334. ```
     
     ```

*pTxBuf ++= 0xFF;

*pTxBuf ++= 0xFF;


336. ```
     
     ```

*pTxBuf ++= (uint8_t)(data_len>>8);

*pTxBuf ++= (uint8_t)(data_len);


338. ```
     
     ```

*pTxBuf ++= head->cmd + 1;

*pTxBuf ++= head->sn;


340. ```
     
     ```

*pTxBuf ++= 0x00;

*pTxBuf ++= proFlag;


342. ```
     
     ```

offset = 8;

if(0x1 == proFlag)


344. ```
     
     ```

{

*pTxBuf ++= sDidLen;


346. ```
     
     ```

offset += 1;

memcpy(&tx_buf[offset],(uint8_t *)head+sizeof(protocolHead_t)+1,sDidLen);


348. ```
     
     ```

offset += sDidLen;

pTxBuf += sDidLen;


350. 
351. ```
     
     ```

}

if(0 != len)


353. ```
     
     ```

{

memcpy(&tx_buf[offset],gizdata,len);


355. ```
     
     ```

}

tx_buf[data_len + 4 - 1 ] = gizProtocolSum( tx_buf , (data_len+4));


357. 
358. ```
     
     ```

ret = uartWrite(tx_buf, data_len+4);

if(ret < 0)


360. ```
     
     ```

{

GIZWITS_LOG("uart write error %d
", ret);


362. ```
     
     ```

return -2;

}


364. 
365. ```
     
     ```

return 0;

  1. }
  2. /**
    • @brief Report data interface
    • @param [in] action : PO action
    • @param [in] data : Payload data
    • @param [in] len : Payload data length
    • @return : 0,Ack success;
  3. -1,Input Param Illegal
  4. -2,Serial send faild
  5. */
  6. static int32_t gizReportData(uint8_t action, uint8_t *gizdata, uint32_t len)
  7. {

int32_t ret = 0;


382. ```
     
     ```

protocolReport_t protocolReport;

if(NULL == gizdata)


385. ```
     
     ```

{

GIZWITS_LOG("gizReportData Error , Illegal Param
");


387. ```
     
     ```

return -1;

}


389. ```
     
     ```

gizProtocolHeadInit((protocolHead_t *)&protocolReport);

protocolReport.head.cmd = CMD_REPORT_P0;


391. ```
     
     ```

protocolReport.head.sn = gizwitsProtocol.sn++;

protocolReport.action = action;


393. ```
     
     ```

protocolReport.head.len = exchangeBytes(sizeof(protocolReport_t)-4);

memcpy((gizwitsReport_t *)&protocolReport.reportData, (gizwitsReport_t *)gizdata,len);


395. ```
     
     ```

protocolReport.sum = gizProtocolSum((uint8_t *)&protocolReport, sizeof(protocolReport_t));

ret = uartWrite((uint8_t *)&protocolReport, sizeof(protocolReport_t));


398. ```
     
     ```

if(ret < 0)

{


400. ```
     
     ```

GIZWITS_LOG("ERR: uart write error %d
", ret);

return -2;


402. ```
     
     ```

}

gizProtocolWaitAck((uint8_t *)&protocolReport, sizeof(protocolReport_t));


405. 
406. ```
     
     ```

return ret;

  1. }/**
    • @brief Datapoints reporting mechanism
      1. Changes are reported immediately
      1. Data timing report , 600000 Millisecond
  2. *@param [in] currentData : Current datapoints value
    • @return : NULL
  3. */
  4. static void gizDevReportPolicy(dataPoint_t *currentData)
  5. {

static uint32_t lastRepTime = 0;


420. ```
     
     ```

uint32_t timeNow = gizGetTimerCount();

if((1 == gizCheckReport(currentData, (dataPoint_t *)&gizwitsProtocol.gizLastDataPoint)))


423. ```
     
     ```

{

GIZWITS_LOG("changed, report data
");


425. ```
     
     ```

if(0 == gizDataPoints2ReportData(currentData,&gizwitsProtocol.reportData.devStatus))

{


427. ```
     
     ```

gizReportData(ACTION_REPORT_DEV_STATUS, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(devStatus_t));        }

memcpy((uint8_t *)&gizwitsProtocol.gizLastDataPoint, (uint8_t *)currentData, sizeof(dataPoint_t));


429. ```
     
     ```

}

if((0 == (timeNow % (600000))) && (lastRepTime != timeNow))


432. ```
     
     ```

{

GIZWITS_LOG("Info: 600S report data
");


434. ```
     
     ```

if(0 == gizDataPoints2ReportData(currentData,&gizwitsProtocol.reportData.devStatus))

{


436. ```
     
     ```

gizReportData(ACTION_REPORT_DEV_STATUS, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(devStatus_t));

}


438. ```
     
     ```

memcpy((uint8_t *)&gizwitsProtocol.gizLastDataPoint, (uint8_t *)currentData, sizeof(dataPoint_t));

lastRepTime = timeNow;


441. ```
     
     ```

}

  1. }
  2. /**
    • @brief Get a packet of data from the ring buffer
    • @param [in] rb : Input data address
    • @param [out] data : Output data address
    • @param [out] len : Output data length
    • @return : 0,Return correct ;-1,Return failure;-2,Data check failure
  3. */
  4. static int8_t gizProtocolGetOnePacket(rb_t *rb, uint8_t *gizdata, uint16_t *len)
  5. {

int32_t ret = 0;


456. ```
     
     ```

uint8_t sum = 0;

int32_t i = 0;


458. ```
     
     ```

uint8_t tmpData;

uint8_t tmpLen = 0;


460. ```
     
     ```

uint16_t tmpCount = 0;

static uint8_t protocolFlag = 0;


462. ```
     
     ```

static uint16_t protocolCount = 0;

static uint8_t lastData = 0;


464. ```
     
     ```

static uint8_t debugCount = 0;

uint8_t *protocolBuff = gizdata;


466. ```
     
     ```

protocolHead_t *head = NULL;

if((NULL == rb) || (NULL == gizdata) ||(NULL == len))


469. ```
     
     ```

{

GIZWITS_LOG("gizProtocolGetOnePacket Error , Illegal Param
");


471. ```
     
     ```

return -1;

}


473. 
474. ```
     
     ```

tmpLen = rbCanRead(rb);

if(0 == tmpLen)


476. ```
     
     ```

{

return -1;


478. ```
     
     ```

}

for(i=0; i<tmpLen; i++)


481. ```
     
     ```

{

ret = rbRead(rb, &tmpData, 1);


483. ```
     
     ```

if(0 != ret)

{


485. ```
     
     ```

if((0xFF == lastData) && (0xFF == tmpData))

{


487. ```
     
     ```

if(0 == protocolFlag)

{


489. ```
     
     ```

protocolBuff[0] = 0xFF;

protocolBuff[1] = 0xFF;


491. ```
     
     ```

protocolCount = 2;

protocolFlag = 1;


493. ```
     
     ```

}

else


495. ```
     
     ```

{

if((protocolCount > 4) && (protocolCount != tmpCount))


497. ```
     
     ```

{

protocolBuff[0] = 0xFF;


499. ```
     
     ```

protocolBuff[1] = 0xFF;

protocolCount = 2;


501. ```
     
     ```

}

}


503. ```
     
     ```

}

else if((0xFF == lastData) && (0x55 == tmpData))


505. ```
     
     ```

{

}


507. ```
     
     ```

else

{


509. ```
     
     ```

if(1 == protocolFlag)

{


511. ```
     
     ```

protocolBuff[protocolCount] = tmpData;

protocolCount++;


513. 
514. ```
     
     ```

if(protocolCount > 4)

{


516. ```
     
     ```

head = (protocolHead_t *)protocolBuff;

tmpCount = exchangeBytes(head->len)+4;


518. ```
     
     ```

if(protocolCount == tmpCount)

{


520. ```
     
     ```

break;

}


522. ```
     
     ```

}

}


524. ```
     
     ```

}

lastData = tmpData;


527. ```
     
     ```

debugCount++;

}


529. ```
     
     ```

}

if((protocolCount > 4) && (protocolCount == tmpCount))


532. ```
     
     ```

{

sum = gizProtocolSum(protocolBuff, protocolCount);


534. 
535. ```
     
     ```

if(protocolBuff[protocolCount-1] == sum)

{


537. ```
     
     ```

memcpy(gizdata, protocolBuff, tmpCount);

*len = tmpCount;


539. ```
     
     ```

protocolFlag = 0;

protocolCount = 0;


542. ```
     
     ```

debugCount = 0;

lastData = 0;


544. 
545. ```
     
     ```

return 0;

}


547. ```
     
     ```

else

{


549. ```
     
     ```

return -2;

}


551. ```
     
     ```

}

return 1;


554. }
555. 
556. 
557. 
558. /**
559. * @brief Protocol data resend
560. 
561. * The protocol data resend when check timeout and meet the resend limiting
562. 
563. * @param none
564. * 
565. * @return none
566. */
567. static void gizProtocolResendData(void)
568. {
569. ```
     
     ```

int32_t ret = 0;

if(0 == gizwitsProtocol.waitAck.flag)


572. ```
     
     ```

{

return;


574. ```
     
     ```

}

GIZWITS_LOG("Warning: timeout, resend data
");


577. 
578. ```
     
     ```

ret = uartWrite(gizwitsProtocol.waitAck.buf, gizwitsProtocol.waitAck.dataLen);

if(ret != gizwitsProtocol.waitAck.dataLen)


580. ```
     
     ```

{

GIZWITS_LOG("ERR: resend data error
");


582. ```
     
     ```

}

gizwitsProtocol.waitAck.sendTime = gizGetTimerCount();


585. }
586. 
587. /**
588. * @brief Clear the ACK protocol message
589. * 
590. * @param [in] head : Protocol header address
591. * 
592. * @return 0, success; other, failure
593. */
594. static int8_t gizProtocolWaitAckCheck(protocolHead_t *head)
595. {
596. ```
     
     ```

protocolHead_t *waitAckHead = (protocolHead_t *)gizwitsProtocol.waitAck.buf;

if(NULL == head)


599. ```
     
     ```

{

GIZWITS_LOG("ERR: data is empty
");


601. ```
     
     ```

return -1;

}


603. 
604. ```
     
     ```

if(waitAckHead->cmd+1 == head->cmd)

{


606. ```
     
     ```

memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));

}


608. 
609. ```
     
     ```

return 0;

  1. }
  2. /**
    • @brief Send general protocol message data
    • @param [in] head : Protocol header address
    • @return : Return effective data length;-1,return failure
  3. */
  4. static int32_t gizProtocolCommonAck(protocolHead_t *head)
  5. {

int32_t ret = 0;


622. ```
     
     ```

protocolCommon_t ack;

if(NULL == head)


625. ```
     
     ```

{

GIZWITS_LOG("ERR: gizProtocolCommonAck data is empty
");


627. ```
     
     ```

return -1;

}


629. ```
     
     ```

memcpy((uint8_t *)&ack, (uint8_t *)head, sizeof(protocolHead_t));

ack.head.cmd = ack.head.cmd+1;


631. ```
     
     ```

ack.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);

ack.sum = gizProtocolSum((uint8_t *)&ack, sizeof(protocolCommon_t));


633. 
634. ```
     
     ```

ret = uartWrite((uint8_t *)&ack, sizeof(protocolCommon_t));

if(ret < 0)


636. ```
     
     ```

{

GIZWITS_LOG("ERR: uart write error %d
", ret);


638. ```
     
     ```

}

return ret;


641. }
642. 
643. /**
644. * @brief ACK processing function
645. 
646. * Time-out 200ms no ACK resend,resend two times at most
647. 
648. * @param none
649. * 
650. * @return none
651. */
652. static void gizProtocolAckHandle(void)
653. {
654. ```
     
     ```

if(1 == gizwitsProtocol.waitAck.flag)

{


656. ```
     
     ```

if(SEND_MAX_NUM > gizwitsProtocol.waitAck.num)

{


658. ```
     
     ```

// Time-out no ACK resend

if(SEND_MAX_TIME < (gizGetTimerCount() - gizwitsProtocol.waitAck.sendTime))


660. ```
     
     ```

{

GIZWITS_LOG("Warning:gizProtocolResendData %d %d %d
", gizGetTimerCount(), gizwitsProtocol.waitAck.sendTime, gizwitsProtocol.waitAck.num);


662. ```
     
     ```

gizProtocolResendData();

gizwitsProtocol.waitAck.num++;


664. ```
     
     ```

}

}


666. ```
     
     ```

else

{


668. ```
     
     ```

memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));

}


670. ```
     
     ```

}

  1. }
  2. /**
    • @brief Protocol 4.1 WiFi module requests device information
    • @param[in] head : Protocol header address
    • @return Return effective data length;-1,return failure
  3. */
  4. static int32_t gizProtocolGetDeviceInfo(protocolHead_t * head)
  5. {

int32_t ret = 0;


683. ```
     
     ```

protocolDeviceInfo_t deviceInfo;

if(NULL == head)


686. ```
     
     ```

{

GIZWITS_LOG("gizProtocolGetDeviceInfo Error , Illegal Param
");


688. ```
     
     ```

return -1;

}


690. 
691. ```
     
     ```

gizProtocolHeadInit((protocolHead_t *)&deviceInfo);

deviceInfo.head.cmd = ACK_GET_DEVICE_INFO;


693. ```
     
     ```

deviceInfo.head.sn = head->sn;

memcpy((uint8_t *)deviceInfo.protocolVer, protocol_VERSION, 8);


695. ```
     
     ```

memcpy((uint8_t *)deviceInfo.p0Ver, P0_VERSION, 8);

memcpy((uint8_t *)deviceInfo.softVer, SOFTWARE_VERSION, 8);


697. ```
     
     ```

memcpy((uint8_t *)deviceInfo.hardVer, HARDWARE_VERSION, 8);

memcpy((uint8_t *)deviceInfo.productKey, PRODUCT_KEY, strlen(PRODUCT_KEY));


699. ```
     
     ```

memcpy((uint8_t *)deviceInfo.productSecret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));

memset((uint8_t *)deviceInfo.devAttr, 0, 8);


701. ```
     
     ```

deviceInfo.devAttr[7] |= DEV_IS_GATEWAY<<0;

deviceInfo.devAttr[7] |= (0x01<<1);


703. ```
     
     ```

deviceInfo.ninableTime = exchangeBytes(NINABLETIME);

deviceInfo.head.len = exchangeBytes(sizeof(protocolDeviceInfo_t)-4);


705. ```
     
     ```

deviceInfo.sum = gizProtocolSum((uint8_t *)&deviceInfo, sizeof(protocolDeviceInfo_t));

ret = uartWrite((uint8_t *)&deviceInfo, sizeof(protocolDeviceInfo_t));


708. ```
     
     ```

if(ret < 0)

{


710. ```
     
     ```

GIZWITS_LOG("ERR: uart write error %d
", ret);

}


712. 
713. ```
     
     ```

return ret;

  1. }
  2. /**
    • @brief Protocol 4.7 Handling of illegal message notification
    • @param[in] head : Protocol header address
    • @param[in] errno : Illegal message notification type
    • @return 0, success; other, failure
  3. */
  4. static int32_t gizProtocolErrorCmd(protocolHead_t *head,errorPacketsType_t errno)
  5. {

int32_t ret = 0;


726. ```
     
     ```

protocolErrorType_t errorType;

if(NULL == head)


729. ```
     
     ```

{

GIZWITS_LOG("gizProtocolErrorCmd Error , Illegal Param
");


731. ```
     
     ```

return -1;

}


733. ```
     
     ```

gizProtocolHeadInit((protocolHead_t *)&errorType);

errorType.head.cmd = ACK_ERROR_PACKAGE;


735. ```
     
     ```

errorType.head.sn = head->sn;

errorType.head.len = exchangeBytes(sizeof(protocolErrorType_t)-4);


738. ```
     
     ```

errorType.error = errno;

errorType.sum = gizProtocolSum((uint8_t *)&errorType, sizeof(protocolErrorType_t));


740. 
741. ```
     
     ```

ret = uartWrite((uint8_t *)&errorType, sizeof(protocolErrorType_t));

if(ret < 0)


743. ```
     
     ```

{

GIZWITS_LOG("ERR: uart write error %d
", ret);


745. ```
     
     ```

}

return ret;


748. }
749. 
750. /**
751. * @brief Protocol 4.13 Get and process network time
752. * 
753. * @param [in] head : Protocol header address
754. * 
755. * @return 0, success; other, failure
756. */
757. static int8_t gizProtocolNTP(protocolHead_t *head)
758. {
759. ```
     
     ```

protocolUTT_t *UTTInfo = (protocolUTT_t *)head;

if(NULL == head)


762. ```
     
     ```

{

GIZWITS_LOG("ERR: NTP is empty
");


764. ```
     
     ```

return -1;

}


766. 
767. ```
     
     ```

memcpy((uint8_t *)&gizwitsProtocol.TimeNTP,(uint8_t *)UTTInfo->time, (7 + 4));

gizwitsProtocol.TimeNTP.year = exchangeBytes(gizwitsProtocol.TimeNTP.year);


769. ```
     
     ```

gizwitsProtocol.TimeNTP.ntp =exchangeWord(gizwitsProtocol.TimeNTP.ntp);

gizwitsProtocol.NTPEvent.event[gizwitsProtocol.NTPEvent.num] = WIFI_NTP;


772. ```
     
     ```

gizwitsProtocol.NTPEvent.num++;

gizwitsProtocol.issuedFlag = GET_NTP_TYPE;


775. 
776. 
777. ```
     
     ```

return 0;

  1. }
  2. /**
    • @brief Protocol 4.4 Device MCU restarts function
    • @param none
    • @return none
  3. */
  4. static void gizProtocolReboot(void)
  5. {

uint32_t timeDelay = gizGetTimerCount();


789. 
790. ```
     
     ```

/*Wait 600ms*/

while((gizGetTimerCount() - timeDelay) <= 600);


792. ```
     
     ```

mcuRestart();

  1. }
  2. /**
    • @brief Protocol 4.5 :The WiFi module informs the device MCU of working status about the WiFi module
    • @param[in] status WiFi module working status
    • @return none
  3. */
  4. static int8_t gizProtocolModuleStatus(protocolWifiStatus_t *status)
  5. {

static wifiStatus_t lastStatus;


804. 
805. ```
     
     ```

if(NULL == status)

{


807. ```
     
     ```

GIZWITS_LOG("gizProtocolModuleStatus Error , Illegal Param
");

return -1;


809. ```
     
     ```

}

status->ststus.value = exchangeBytes(status->ststus.value);


812. 
813. ```
     
     ```

//OnBoarding mode status

if(lastStatus.types.onboarding != status->ststus.types.onboarding)


815. ```
     
     ```

{

if(1 == status->ststus.types.onboarding)


817. ```
     
     ```

{

if(1 == status->ststus.types.softap)


819. ```
     
     ```

{

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_SOFTAP;


821. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.num++;

GIZWITS_LOG("OnBoarding: SoftAP or Web mode
");


823. ```
     
     ```

}

if(1 == status->ststus.types.station)


826. ```
     
     ```

{

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_AIRLINK;


828. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.num++;

GIZWITS_LOG("OnBoarding: AirLink mode
");


830. ```
     
     ```

}

}


832. ```
     
     ```

else

{


834. ```
     
     ```

if(1 == status->ststus.types.softap)

{


836. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_SOFTAP;

gizwitsProtocol.wifiStatusEvent.num++;


838. ```
     
     ```

GIZWITS_LOG("OnBoarding: SoftAP or Web mode
");

}


840. 
841. ```
     
     ```

if(1 == status->ststus.types.station)

{


843. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_STATION;

gizwitsProtocol.wifiStatusEvent.num++;


845. ```
     
     ```

GIZWITS_LOG("OnBoarding: Station mode
");

}


847. ```
     
     ```

}

}


849. 
850. ```
     
     ```

//binding mode status

if(lastStatus.types.binding != status->ststus.types.binding)


852. ```
     
     ```

{

lastStatus.types.binding = status->ststus.types.binding;


854. ```
     
     ```

if(1 == status->ststus.types.binding)

{


856. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_OPEN_BINDING;

gizwitsProtocol.wifiStatusEvent.num++;


858. ```
     
     ```

GIZWITS_LOG("WiFi status: in binding mode
");

}


860. ```
     
     ```

else

{


862. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CLOSE_BINDING;

gizwitsProtocol.wifiStatusEvent.num++;


864. ```
     
     ```

GIZWITS_LOG("WiFi status: out binding mode
");

}


866. ```
     
     ```

}

//router status


869. ```
     
     ```

if(lastStatus.types.con_route != status->ststus.types.con_route)

{


871. ```
     
     ```

lastStatus.types.con_route = status->ststus.types.con_route;

if(1 == status->ststus.types.con_route)


873. ```
     
     ```

{

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_ROUTER;


875. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.num++;

GIZWITS_LOG("WiFi status: connected router
");


877. ```
     
     ```

}

else


879. ```
     
     ```

{

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_ROUTER;


881. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.num++;

GIZWITS_LOG("WiFi status: disconnected router
");


883. ```
     
     ```

}

}


885. 
886. ```
     
     ```

//M2M server status

if(lastStatus.types.con_m2m != status->ststus.types.con_m2m)


888. ```
     
     ```

{

lastStatus.types.con_m2m = status->ststus.types.con_m2m;


890. ```
     
     ```

if(1 == status->ststus.types.con_m2m)

{


892. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_M2M;

gizwitsProtocol.wifiStatusEvent.num++;


894. ```
     
     ```

GIZWITS_LOG("WiFi status: connected m2m
");

}


896. ```
     
     ```

else

{


898. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_M2M;

gizwitsProtocol.wifiStatusEvent.num++;


900. ```
     
     ```

GIZWITS_LOG("WiFi status: disconnected m2m
");

}


902. ```
     
     ```

}

//APP status


905. ```
     
     ```

if(lastStatus.types.app != status->ststus.types.app)

{


907. ```
     
     ```

lastStatus.types.app = status->ststus.types.app;

if(1 == status->ststus.types.app)


909. ```
     
     ```

{

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_APP;


911. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.num++;

GIZWITS_LOG("WiFi status: app connect
");


913. ```
     
     ```

}

else


915. ```
     
     ```

{

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_APP;


917. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.num++;

GIZWITS_LOG("WiFi status: no app connect
");


919. ```
     
     ```

}

}


921. 
922. ```
     
     ```

//test mode status

if(lastStatus.types.test != status->ststus.types.test)


924. ```
     
     ```

{

lastStatus.types.test = status->ststus.types.test;


926. ```
     
     ```

if(1 == status->ststus.types.test)

{


928. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_OPEN_TESTMODE;

gizwitsProtocol.wifiStatusEvent.num++;


930. ```
     
     ```

GIZWITS_LOG("WiFi status: in test mode
");

}


932. ```
     
     ```

else

{


934. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CLOSE_TESTMODE;

gizwitsProtocol.wifiStatusEvent.num++;


936. ```
     
     ```

GIZWITS_LOG("WiFi status: out test mode
");

}


938. ```
     
     ```

}

gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_RSSI;


941. ```
     
     ```

gizwitsProtocol.wifiStatusEvent.num++;

gizwitsProtocol.wifiStatusData.rssi = status->ststus.types.rssi;


943. ```
     
     ```

GIZWITS_LOG("RSSI is %d
", gizwitsProtocol.wifiStatusData.rssi);

gizwitsProtocol.issuedFlag = WIFI_STATUS_TYPE;


946. 
947. ```
     
     ```

return 0;

  1. }
  2. /**@name Gizwits User API interface
    • @{
  3. */
  4. /**
    • @brief gizwits Protocol initialization interface
    • Protocol-related timer, serial port initialization
    • Datapoint initialization
    • @param none
    • @return none
  5. */
  6. void gizwitsInit(void)
  7. {

pRb.rbCapacity = RB_MAX_LEN;


968. ```
     
     ```

pRb.rbBuff = rbBuf;

if(0 == rbCreate(&pRb))


970. ```
     
     ```

{

GIZWITS_LOG("rbCreate Success
");


972. ```
     
     ```

}

else


974. ```
     
     ```

{

GIZWITS_LOG("rbCreate Faild
");


976. ```
     
     ```

}

memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));


979. }
980. 
981. /**
982. * @brief WiFi configure interface
983. 
984. * Set the WiFi module into the corresponding configuration mode or reset the module
985. 
986. * @param[in] mode :0x0, reset the module ;0x01, SoftAp mode ;0x02, AirLink mode ;0x03, Production test mode; 0x04:allow users to bind devices
987. 
988. * @return Error command code
989. */
990. int32_t gizwitsSetMode(uint8_t mode)
991. {
992. ```
     
     ```

int32_t ret = 0;

protocolCfgMode_t cfgMode;


994. ```
     
     ```

protocolCommon_t setDefault;

switch(mode)


997. ```
     
     ```

{

case WIFI_RESET_MODE:


999. ```
     
     ```

gizProtocolHeadInit((protocolHead_t *)&setDefault);

setDefault.head.cmd = CMD_SET_DEFAULT;


1001. ```
      
      ```

setDefault.head.sn = gizwitsProtocol.sn++;

setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);


1003. ```
      
      ```

setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));

ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));


1005. ```
      
      ```

if(ret < 0)

{


1007. ```
      
      ```

GIZWITS_LOG("ERR: uart write error %d
", ret);

}


1009. 
1010. ```
      
      ```

gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));

break;


1012. ```
      
      ```

case WIFI_SOFTAP_MODE:

gizProtocolHeadInit((protocolHead_t *)&cfgMode);


1014. ```
      
      ```

cfgMode.head.cmd = CMD_WIFI_CONFIG;

cfgMode.head.sn = gizwitsProtocol.sn++;


1016. ```
      
      ```

cfgMode.cfgMode = mode;

cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);


1018. ```
      
      ```

cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));

ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));


1020. ```
      
      ```

if(ret < 0)

{


1022. ```
      
      ```

GIZWITS_LOG("ERR: uart write error %d
", ret);

}


1024. ```
      
      ```

gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));

break;


1026. ```
      
      ```

case WIFI_AIRLINK_MODE:

gizProtocolHeadInit((protocolHead_t *)&cfgMode);


1028. ```
      
      ```

cfgMode.head.cmd = CMD_WIFI_CONFIG;

cfgMode.head.sn = gizwitsProtocol.sn++;


1030. ```
      
      ```

cfgMode.cfgMode = mode;

cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);


1032. ```
      
      ```

cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));

ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));


1034. ```
      
      ```

if(ret < 0)

{


1036. ```
      
      ```

GIZWITS_LOG("ERR: uart write error %d
", ret);

}


1038. ```
      
      ```

gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));

break;


1040. ```
      
      ```

case WIFI_PRODUCTION_TEST:

gizProtocolHeadInit((protocolHead_t *)&setDefault);


1042. ```
      
      ```

setDefault.head.cmd = CMD_PRODUCTION_TEST;

setDefault.head.sn = gizwitsProtocol.sn++;


1044. ```
      
      ```

setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);

setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));


1046. ```
      
      ```

ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));

if(ret < 0)


1048. ```
      
      ```

{

GIZWITS_LOG("ERR: uart write error %d
", ret);


1050. ```
      
      ```

}

gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));


1053. ```
      
      ```

break;

case WIFI_NINABLE_MODE:


1055. ```
      
      ```

gizProtocolHeadInit((protocolHead_t *)&setDefault);

setDefault.head.cmd = CMD_NINABLE_MODE;


1057. ```
      
      ```

setDefault.head.sn = gizwitsProtocol.sn++;

setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);


1059. ```
      
      ```

setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));

ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));


1061. ```
      
      ```

if(ret < 0)

{


1063. ```
      
      ```

GIZWITS_LOG("ERR: uart write error %d
", ret);

}


1065. 
1066. ```
      
      ```

gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));

break;


1068. ```
      
      ```

case WIFI_REBOOT_MODE:

gizProtocolHeadInit((protocolHead_t *)&setDefault);


1070. ```
      
      ```

setDefault.head.cmd = CMD_REBOOT_MODULE;

setDefault.head.sn = gizwitsProtocol.sn++;


1072. ```
      
      ```

setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);

setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));


1074. ```
      
      ```

ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));

if(ret < 0)


1076. ```
      
      ```

{

GIZWITS_LOG("ERR: uart write error %d
", ret);


1078. ```
      
      ```

}

gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));


1081. ```
      
      ```

break;

default:


1083. ```
      
      ```

GIZWITS_LOG("ERR: CfgMode error!
");

break;


1085. ```
      
      ```

}

return ret;


1088. }
1089. 
1090. /**
1091. * @brief Get the the network time
1092. 
1093. * Protocol 4.13:"Device MCU send" of "the MCU requests access to the network time"
1094. 
1095. * @param[in] none
1096. * @return none
1097. */
1098. void gizwitsGetNTP(void)
1099. {
1100. ```
      
      ```

int32_t ret = 0;

protocolCommon_t getNTP;


1102. 
1103. ```
      
      ```

gizProtocolHeadInit((protocolHead_t *)&getNTP);

getNTP.head.cmd = CMD_GET_NTP;


1105. ```
      
      ```

getNTP.head.sn = gizwitsProtocol.sn++;

getNTP.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);


1107. ```
      
      ```

getNTP.sum = gizProtocolSum((uint8_t *)&getNTP, sizeof(protocolCommon_t));

ret = uartWrite((uint8_t *)&getNTP, sizeof(protocolCommon_t));


1109. ```
      
      ```

if(ret < 0)

{


1111. ```
      
      ```

GIZWITS_LOG("ERR[NTP]: uart write error %d
", ret);

}


1113. 
1114. ```
      
      ```

gizProtocolWaitAck((uint8_t *)&getNTP, sizeof(protocolCommon_t));

  1. }
  2. /**
    • @brief Get Module Info
    • @param[in] none
    • @return none
  3. */
  4. void gizwitsGetModuleInfo(void)
  5. {

int32_t ret = 0;


1129. ```
      
      ```

protocolGetModuleInfo_t getModuleInfo;

gizProtocolHeadInit((protocolHead_t *)&getModuleInfo);


1132. ```
      
      ```

getModuleInfo.head.cmd = CMD_ASK_MODULE_INFO;

getModuleInfo.head.sn = gizwitsProtocol.sn++;


1134. ```
      
      ```

getModuleInfo.type = 0x0;

getModuleInfo.head.len = exchangeBytes(sizeof(protocolGetModuleInfo_t)-4);


1136. ```
      
      ```

getModuleInfo.sum = gizProtocolSum((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));

ret = uartWrite((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));


1138. ```
      
      ```

if(ret < 0)

{


1140. ```
      
      ```

GIZWITS_LOG("ERR[NTP]: uart write error %d
", ret);

}


1142. 
1143. ```
      
      ```

gizProtocolWaitAck((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));

  1. }
  2. /**
    • @brief Module Info Analyse
    • @param [in] head :
    • @return 0, Success, , other,Faild
  3. */
  4. static int8_t gizProtocolModuleInfoHandle(protocolHead_t *head)
  5. {

protocolModuleInfo_t *moduleInfo = (protocolModuleInfo_t *)head;


1157. 
1158. ```
      
      ```

if(NULL == head)

{


1160. ```
      
      ```

GIZWITS_LOG("NTP is empty
");

return -1;


1162. ```
      
      ```

}

memcpy((uint8_t *)&gizwitsProtocol.wifiModuleNews,(uint8_t *)&moduleInfo->wifiModuleInfo, sizeof(moduleInfo_t));


1165. 
1166. ```
      
      ```

gizwitsProtocol.moduleInfoEvent.event[gizwitsProtocol.moduleInfoEvent.num] = MODULE_INFO;

gizwitsProtocol.moduleInfoEvent.num++;


1168. 
1169. ```
      
      ```

gizwitsProtocol.issuedFlag = GET_MODULEINFO_TYPE;

return 0;


1173. }
1174. 
1175. /**
1176. * @brief Protocol handling function
1177. 
1178. * 
1179. 
1180. * @param [in] currentData :The protocol data pointer
1181. * @return none
1182. */
1183. int32_t gizwitsHandle(dataPoint_t *currentData)
1184. {
1185. ```
      
      ```

int8_t ret = 0;

  1. #ifdef PROTOCOL_DEBUG

uint16_t i = 0;


1188. #endif
1189. ```
      
      ```

uint8_t ackData[RB_MAX_LEN];

uint16_t protocolLen = 0;


1191. ```
      
      ```

uint32_t ackLen = 0;

protocolHead_t *recvHead = NULL;


1193. ```
      
      ```

char *didPtr = NULL;

uint16_t offset = 0;


1195. 
1196. 
1197. ```
      
      ```

if(NULL == currentData)

{


1199. ```
      
      ```

GIZWITS_LOG("GizwitsHandle Error , Illegal Param
");

return -1;


1201. ```
      
      ```

}

/ resend strategy /


1204. ```
      
      ```

gizProtocolAckHandle();

ret = gizProtocolGetOnePacket(&pRb, gizwitsProtocol.protocolBuf, &protocolLen);


1206. 
1207. ```
      
      ```

if(0 == ret)

{


1209. ```
      
      ```

GIZWITS_LOG("Get One Packet!
");

  1. #ifdef PROTOCOL_DEBUG

GIZWITS_LOG("WiFi2MCU[%4d:%4d]: ", gizGetTimerCount(), protocolLen);


1213. ```
      
      ```

for(i=0; i<protocolLen;i++)

{


1215. ```
      
      ```

GIZWITS_LOG("%02x ", gizwitsProtocol.protocolBuf[i]);

}


1217. ```
      
      ```

GIZWITS_LOG("
");

  1. #endif

recvHead = (protocolHead_t *)gizwitsProtocol.protocolBuf;


1221. ```
      
      ```

switch (recvHead->cmd)

{


1223. ```
      
      ```

case CMD_GET_DEVICE_INTO:

gizProtocolGetDeviceInfo(recvHead);


1225. ```
      
      ```

break;

case CMD_ISSUED_P0:


1227. ```
      
      ```

GIZWITS_LOG("flag %x %x
", recvHead->flags[0], recvHead->flags[1]);

//offset = 1;


1229. 
1230. ```
      
      ```

if(0 == gizProtocolIssuedProcess(didPtr, gizwitsProtocol.protocolBuf+sizeof(protocolHead_t)+offset, protocolLen-(sizeof(protocolHead_t)+offset+1), ackData, &ackLen))

{


1232. ```
      
      ```

gizProtocolIssuedDataAck(recvHead, ackData, ackLen,recvHead->flags[1]);

GIZWITS_LOG("AckData :
");


1234. ```
      
      ```

}

break;


1236. ```
      
      ```

case CMD_HEARTBEAT:

gizProtocolCommonAck(recvHead);


1238. ```
      
      ```

break;

case CMD_WIFISTATUS:


1240. ```
      
      ```

gizProtocolCommonAck(recvHead);

gizProtocolModuleStatus((protocolWifiStatus_t *)recvHead);


1242. ```
      
      ```

break;

case ACK_REPORT_P0:


1244. ```
      
      ```

case ACK_WIFI_CONFIG:

case ACK_SET_DEFAULT:


1246. ```
      
      ```

case ACK_NINABLE_MODE:

case ACK_REBOOT_MODULE:


1248. ```
      
      ```

gizProtocolWaitAckCheck(recvHead);

break;


1250. ```
      
      ```

case CMD_MCU_REBOOT:

gizProtocolCommonAck(recvHead);


1252. ```
      
      ```

GIZWITS_LOG("report:MCU reboot!
");

gizProtocolReboot();


1255. ```
      
      ```

break;

case CMD_ERROR_PACKAGE:


1257. ```
      
      ```

break;

case ACK_PRODUCTION_TEST:


1259. ```
      
      ```

gizProtocolWaitAckCheck(recvHead);

GIZWITS_LOG("Ack PRODUCTION_MODE success
");


1261. ```
      
      ```

break;

case ACK_GET_NTP:


1263. ```
      
      ```

gizProtocolWaitAckCheck(recvHead);

gizProtocolNTP(recvHead);


1265. ```
      
      ```

GIZWITS_LOG("Ack GET_UTT success
");

break;


1267. ```
      
      ```

case ACK_ASK_MODULE_INFO:

gizProtocolWaitAckCheck(recvHead);


1269. ```
      
      ```

gizProtocolModuleInfoHandle(recvHead);

GIZWITS_LOG("Ack GET_Module success
");


1271. ```
      
      ```

break;

default:


1274. ```
      
      ```

gizProtocolErrorCmd(recvHead,ERROR_CMD);

GIZWITS_LOG("ERR: cmd code error!
");


1276. ```
      
      ```

break;

}


1278. ```
      
      ```

}

else if(-2 == ret)


1280. ```
      
      ```

{

//Check failed, report exception


1282. ```
      
      ```

recvHead = (protocolHead_t *)gizwitsProtocol.protocolBuf;

gizProtocolErrorCmd(recvHead,ERROR_ACK_SUM);


1284. ```
      
      ```

GIZWITS_LOG("ERR: check sum error!
");

return -2;


1286. ```
      
      ```

}

switch(gizwitsProtocol.issuedFlag)


1289. ```
      
      ```

{

case ACTION_CONTROL_TYPE:


1291. ```
      
      ```

gizwitsProtocol.issuedFlag = STATELESS_TYPE;

gizwitsEventProcess(&gizwitsProtocol.issuedProcessEvent, (uint8_t *)&gizwitsProtocol.gizCurrentDataPoint, sizeof(dataPoint_t));


1293. ```
      
      ```

memset((uint8_t *)&gizwitsProtocol.issuedProcessEvent,0x0,sizeof(gizwitsProtocol.issuedProcessEvent));

break;


1295. ```
      
      ```

case WIFI_STATUS_TYPE:

gizwitsProtocol.issuedFlag = STATELESS_TYPE;


1297. ```
      
      ```

gizwitsEventProcess(&gizwitsProtocol.wifiStatusEvent, (uint8_t *)&gizwitsProtocol.wifiStatusData, sizeof(moduleStatusInfo_t));

memset((uint8_t *)&gizwitsProtocol.wifiStatusEvent,0x0,sizeof(gizwitsProtocol.wifiStatusEvent));


1299. ```
      
      ```

break;

case ACTION_W2D_TRANSPARENT_TYPE:


1301. ```
      
      ```

gizwitsProtocol.issuedFlag = STATELESS_TYPE;

gizwitsEventProcess(&gizwitsProtocol.issuedProcessEvent, (uint8_t *)gizwitsProtocol.transparentBuff, gizwitsProtocol.transparentLen);


1303. ```
      
      ```

break;

case GET_NTP_TYPE:


1305. ```
      
      ```

gizwitsProtocol.issuedFlag = STATELESS_TYPE;

gizwitsEventProcess(&gizwitsProtocol.NTPEvent, (uint8_t *)&gizwitsProtocol.TimeNTP, sizeof(protocolTime_t));


1307. ```
      
      ```

memset((uint8_t *)&gizwitsProtocol.NTPEvent,0x0,sizeof(gizwitsProtocol.NTPEvent));

break;


1309. ```
      
      ```

case GET_MODULEINFO_TYPE:

gizwitsProtocol.issuedFlag = STATELESS_TYPE;


1311. ```
      
      ```

gizwitsEventProcess(&gizwitsProtocol.moduleInfoEvent, (uint8_t *)&gizwitsProtocol.wifiModuleNews, sizeof(moduleInfo_t));

memset((uint8_t *)&gizwitsProtocol.moduleInfoEvent,0x0,sizeof(moduleInfo_t));


1313. ```
      
      ```

break;

default:


1315. ```
      
      ```

break;

}


1317. 
1318. ```
      
      ```

gizDevReportPolicy(currentData);

return 0;


1321. }
1322. 
1323. /**
1324. * @brief gizwits report transparent data interface
1325. 
1326. * The user can call the interface to complete the reporting of private protocol data
1327. 
1328. * @param [in] data :Private protocol data
1329. * @param [in] len  :Private protocol data length
1330. * @return 0,success ;other,failure
1331. */
1332. int32_t gizwitsPassthroughData(uint8_t * gizdata, uint32_t len)
1333. {
1334. ```
      
      ```

int32_t ret = 0;

uint8_t tx_buf[MAX_PACKAGE_LEN];


1336. ```
      
      ```

uint8_t *pTxBuf = tx_buf;

uint16_t data_len = 6+len;


1338. ```
      
      ```

if(NULL == gizdata)

{


1340. ```
      
      ```

GIZWITS_LOG("[ERR] gizwitsPassthroughData Error
");

return (-1);


1342. ```
      
      ```

}

*pTxBuf ++= 0xFF;


1345. ```
      
      ```

*pTxBuf ++= 0xFF;

*pTxBuf ++= (uint8_t)(data_len>>8);//len


1347. ```
      
      ```

*pTxBuf ++= (uint8_t)(data_len);

*pTxBuf ++= CMD_REPORT_P0;//0x1b cmd


1349. ```
      
      ```

*pTxBuf ++= gizwitsProtocol.sn++;//sn

*pTxBuf ++= 0x00;//flag


1351. ```
      
      ```

*pTxBuf ++= 0x00;//flag

*pTxBuf ++= ACTION_D2W_TRANSPARENT_DATA;//P0_Cmd


1353. 
1354. ```
      
      ```

memcpy(&tx_buf[9],gizdata,len);

tx_buf[data_len + 4 - 1 ] = gizProtocolSum( tx_buf , (data_len+4));


1356. 
1357. ```
      
      ```

ret = uartWrite(tx_buf, data_len+4);

if(ret < 0)


1359. ```
      
      ```

{

GIZWITS_LOG("ERR: uart write error %d
", ret);


1361. ```
      
      ```

}

gizProtocolWaitAck(tx_buf, data_len+4);


1364. 
1365. ```
      
      ```

return 0;

  1. }
  2. void gziwits_Task(dataPoint_t * currentDataPoint)
  3. {

static uint32_t Timer=0;


1372. ```
      
      ```

if(SoftTimer(Timer,5000))

{


1374. ```
      
      ```

gizwitsHandle(currentDataPoint);

Timer=GetSoftTimer();


1376. ```
      
      ```

}

  1. }
  2. /**@} */

复制代码

修改gizwits_protocol.h

  1. #ifndef _GIZWITS_PROTOCOL_H
  2. #define _GIZWITS_PROTOCOL_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "common.h"
  12. #define SEND_MAX_TIME 200 ///< 200ms resend
  13. #define SEND_MAX_NUM 2 ///< resend times
  14. #define protocol_VERSION "00000004" ///< protocol version
  15. #define P0_VERSION "00000002" ///< P0 protocol version
  16. /**@name Product Key
    • @{
  17. */
  18. #define PRODUCT_KEY "9c8a5a8e38344fb4af14b6db0f5b1df7"
  19. /**@} */
  20. /**@name Product Secret
    • @{
  21. */
  22. #define PRODUCT_SECRET "45c86d8c6a2a4b1dac7d68df54f6e4f0"
  23. /**@name Device status data reporting interval
    • @{
  24. */
  25. #define REPORT_TIME_MAX 6000 //6S
  26. /**@} */
  27. #define CELLNUMMAX 7
  28. /**@name Whether the device is in the control class, 0 means no, 1 means yes
    • @{
  29. */
  30. #define DEV_IS_GATEWAY 0
  31. /**@} */
  32. /**@name Binding time
    • @{
  33. */
  34. #define NINABLETIME 0
  35. /**@} */
  36. #define MAX_PACKAGE_LEN (sizeof(devStatus_t)+sizeof(attrFlags_t)+20) ///< Data buffer maximum length
  37. #define RB_MAX_LEN (MAX_PACKAGE_LEN*2) ///< Maximum length of ring buffer
  38. /**@name Data point related definition
    • @{
  39. */
  40. #define Relay_1_BYTEOFFSET 0
  41. #define Relay_1_BITOFFSET 0
  42. #define Relay_1_LEN 1
  43. #define Temp_RATIO 1
  44. #define Temp_ADDITION 0
  45. #define Temp_MIN 0
  46. #define Temp_MAX 100
  47. #define Humi_RATIO 1
  48. #define Humi_ADDITION 0
  49. #define Humi_MIN 0
  50. #define Humi_MAX 100
  51. #define Light_Intensity_RATIO 1
  52. #define Light_Intensity_ADDITION 0
  53. #define Light_Intensity_MIN 0
  54. #define Light_Intensity_MAX 100
  55. /**@} */
  56. /** Writable data points Boolean and enumerated variables occupy byte size */
  57. #define COUNT_W_BIT 1
  58. /** Event enumeration */
  59. typedef enum
  60. {
  61. WIFI_SOFTAP = 0x00, ///< WiFi SOFTAP configuration event
  62. WIFI_AIRLINK, ///< WiFi module AIRLINK configuration event
  63. WIFI_STATION, ///< WiFi module STATION configuration event
  64. WIFI_OPEN_BINDING, ///< The WiFi module opens the binding event
  65. WIFI_CLOSE_BINDING, ///< The WiFi module closes the binding event
  66. WIFI_CON_ROUTER, ///< The WiFi module is connected to a routing event
  67. WIFI_DISCON_ROUTER, ///< The WiFi module has been disconnected from the routing event
  68. WIFI_CON_M2M, ///< The WiFi module has a server M2M event
  69. WIFI_DISCON_M2M, ///< The WiFi module has been disconnected from the server M2M event
  70. WIFI_OPEN_TESTMODE, ///< The WiFi module turns on the test mode event
  71. WIFI_CLOSE_TESTMODE, ///< The WiFi module turns off the test mode event
  72. WIFI_CON_APP, ///< The WiFi module connects to the APP event
  73. WIFI_DISCON_APP, ///< The WiFi module disconnects the APP event
  74. WIFI_RSSI, ///< WiFi module RSSI event
  75. WIFI_NTP, ///< Network time event
  76. MODULE_INFO, ///< Module information event
  77. TRANSPARENT_DATA, ///< Transparency events
  78. EVENT_Relay_1,
  79. EVENT_TYPE_MAX ///< Enumerate the number of members to calculate (user accidentally deleted)
  80. } EVENT_TYPE_T;
  81. /** P0 Command code */
  82. typedef enum
  83. {

ACTION_CONTROL_DEVICE       = 0x01,             ///< Protocol 4.10 WiFi Module Control Device WiFi Module Send

ACTION_READ_DEV_STATUS = 0x02, ///< Protocol 4.8 WiFi Module Reads the current status of the device WiFi module sent


ACTION_READ_DEV_STATUS_ACK  = 0x03,             ///< Protocol 4.8 WiFi Module Read Device Current Status Device MCU Reply

ACTION_REPORT_DEV_STATUS = 0x04, ///< Protocol 4.9 device MCU to the WiFi module to actively report the current status of the device to send the MCU


ACTION_W2D_TRANSPARENT_DATA = 0x05,             ///< WiFi to device MCU transparent

ACTION_D2W_TRANSPARENT_DATA = 0x06, ///< Device MCU to WiFi


121. } actionType_t;
122. 
123. /** Protocol network time structure */
124. typedef struct
125. {
126. ```
     
     ```

uint16_t year;

uint8_t month;


uint8_t day;

uint8_t hour;


uint8_t minute;

uint8_t second;


uint32_t ntp;

  1. }protocolTime_t;
  2. /** WiFi Module configuration parameters*/
  3. typedef enum
  4. {
  5. WIFI_RESET_MODE = 0x00, ///< WIFI module reset
  6. WIFI_SOFTAP_MODE, ///< WIFI module softAP modeF
  7. WIFI_AIRLINK_MODE, ///< WIFI module AirLink mode
  8. WIFI_PRODUCTION_TEST, ///< MCU request WiFi module into production test mode
  9. WIFI_NINABLE_MODE, ///< MCU request module to enter binding mode
  10. WIFI_REBOOT_MODE, ///< MCU request module reboot
  11. }WIFI_MODE_TYPE_T;
  12. /** The protocol event type*/
  13. typedef enum
  14. {
  15. STATELESS_TYPE = 0x00, ///< Stateless type
  16. ACTION_CONTROL_TYPE, ///< Protocol 4.10 :WiFi module control device event
  17. WIFI_STATUS_TYPE, ///< Protocol 4.5 :WiFi module inform the device MCU of the change event of the WiFi module status
  18. ACTION_W2D_TRANSPARENT_TYPE, ///< Protocol WiFi to device MCU transparent event
  19. GET_NTP_TYPE, ///< Protocol 4.13 :The MCU requests access to the network time event
  20. GET_MODULEINFO_TYPE, ///< Protocol 4.9 :The MCU get module information event
  21. PROTOCOL_EVENT_TYPE_MAX ///< Count enumerated member (User donot delete)
  22. } PROTOCOL_EVENT_TYPE_T;
  23. /** Protocol command code */
  24. typedef enum
  25. {

CMD_GET_DEVICE_INTO = 0x01, ///< Protocol:3.1


ACK_GET_DEVICE_INFO             = 0x02,         ///< Protocol:3.1

CMD_ISSUED_P0 = 0x03, ///< Protocol:3.2 3.3


ACK_ISSUED_P0                   = 0x04,         ///< Protocol:3.2 3.3

CMD_REPORT_P0 = 0x05, ///< Protocol:3.4


ACK_REPORT_P0                   = 0x06,         ///< Protocol:3.4

CMD_HEARTBEAT = 0x07, ///< Protocol:3.5


ACK_HEARTBEAT                   = 0x08,         ///< Protocol:3.5

CMD_WIFI_CONFIG = 0x09, ///< Protocol:3.6


ACK_WIFI_CONFIG                 = 0x0A,         ///< Protocol:3.6

CMD_SET_DEFAULT = 0x0B, ///< Protocol:3.7


ACK_SET_DEFAULT                 = 0x0C,         ///< Protocol:3.7

CMD_WIFISTATUS = 0x0D, ///< Protocol:3.8


ACK_WIFISTATUS                  = 0x0E,         ///< Protocol:3.8

CMD_MCU_REBOOT = 0x0F, ///< Protocol:4.1


ACK_MCU_REBOOT                  = 0x10,         ///< Protocol:4.1

CMD_ERROR_PACKAGE = 0x11, ///< Protocol:3.9


ACK_ERROR_PACKAGE               = 0x12,         ///< Protocol:3.9

CMD_PRODUCTION_TEST = 0x13, ///< Protocol:


ACK_PRODUCTION_TEST             = 0x14,         ///< Protocol:

CMD_NINABLE_MODE = 0x15, ///< Protocol:3.10


ACK_NINABLE_MODE                = 0x16,         ///< Protocol:3.10

CMD_GET_NTP = 0x17, ///< Protocol:4.3


ACK_GET_NTP                     = 0x18,         ///< Protocol:4.3

CMD_ASK_BIGDATA = 0x19, ///< Protocol:4.4


ACK_ASK_BIGDATA                 = 0x1A,         ///< Protocol:4.4

CMD_BIGDATA_READY = 0x1B, ///< Protocol:4.5


ACK_BIGDATA_READY               = 0x1C,         ///< Protocol:4.5

CMD_BIGDATA_SEND = 0x1D, ///< Protocol:4.6


ACK_BIGDATA_SEND                = 0x1E,         ///< Protocol:4.6

CMD_S_STOP_BIGDATA_SEND = 0x1F, ///< Protocol:4.7


ACK_S_STOP_BIGDATA_SEND         = 0x20,         ///< Protocol:4.7

CMD_D_STOP_BIGDATA_SEND = 0x27, ///< Protocol:4.8


ACK_D_STOP_BIGDATA_SEND         = 0x28,         ///< Protocol:4.8

CMD_ASK_MODULE_INFO = 0x21, ///< Protocol:4.9


ACK_ASK_MODULE_INFO             = 0x22,         ///< Protocol:4.9

CMD_ASK_AFFAIR_HANDLE = 0x23, ///< Protocol:4.10


ACK_ASK_AFFAIR_HANDLE           = 0x24,         ///< Protocol:4.10

CMD_AFFAIR_RESULT = 0x25, ///< Protocol:4.10


ACK_AFFAIR_RESULT               = 0x26,         ///< Protocol:4.10

CMD_REBOOT_MODULE = 0x29, ///< Protocol:3.11


ACK_REBOOT_MODULE               = 0x2A,         ///< Protocol:3.11

CMD_CONNECT_M2M = 0x2D, ///< Protocol:for Virtualization


ACK_CONNECT_M2M                 = 0x2E,         ///< Protocol:for Virtualization

CMD_CONNECT_M2M_BACK = 0x2F, ///< Protocol:for Virtualization


ACK_CONNECT_M2M_BACK            = 0x30,         ///< Protocol:for Virtualization

CMD_UPLOAD_DATA = 0x31, ///< Protocol:for Virtualization


ACK_UPLOAD_DATA                 = 0x32,         ///< Protocol:for Virtualization

CMD_UPLOAD_DATA_BACK = 0x33, ///< Protocol:for Virtualization


ACK_UPLOAD_DATA_BACK            = 0x34,         ///< Protocol:for Virtualization

CMD_DISCONNECT_M2M = 0x35, ///< Protocol:for Virtualization


ACK_DISCONNECT_M2M              = 0x36,         ///< Protocol:for Virtualization

CMD_DISCONNECT_M2M_BACK = 0x37, ///< Protocol:for Virtualization


ACK_DISCONNECT_M2M_BACK         = 0x38,         ///< Protocol:for Virtualization

CMD_RESET_SIMULATOR = 0x39, ///< Protocol:for Virtualization


ACK_RESET_SIMULATOR             = 0x3A,         ///< Protocol:for Virtualization

CMD_RESET_SIMULATOR_BACK = 0x3B, ///< Protocol:for Virtualization


ACK_RESET_SIMULATOR_BACK        = 0x3C,         ///< Protocol:for Virtualization

  1. } PROTOCOL_CMDTYPE;
  2. /** Illegal message type*/
  3. typedef enum
  4. {

ERROR_ACK_SUM = 0x01, ///< check error


ERROR_CMD     = 0x02,                           ///< Command code error

ERROR_OTHER = 0x03, ///< other


257. } errorPacketsType_t;
258. 
259. typedef enum
260. {
261. ```
     
     ```

EXE_SUCESS                      = 0x00,

EXE_FAILE = 0x01,


263. } execute_result;
264. 
265. #pragma pack(1)
266. 
267. /** User Area Device State Structure */
268. typedef struct {
269. bool valueRelay_1;
270. uint32_t valueTemp;
271. uint32_t valueHumi;
272. uint32_t valueLight_Intensity;
273. } dataPoint_t;
274. 
275. 
276. /** Corresponding to the protocol "4.10 WiFi module control device" in the flag " attr_flags" */
277. typedef struct {
278. uint8_t flagRelay_1:1;
279. } attrFlags_t;
280. 
281. 
282. /** Corresponding protocol "4.10 WiFi module control device" in the data value "attr_vals" */
283. 
284. typedef struct {
285. uint8_t wBitBuf[COUNT_W_BIT];
286. } attrVals_t;
287. 
288. /** The flag "attr_flags (1B)" + data value "P0 protocol area" in the corresponding protocol "4.10 WiFi module control device"attr_vals(6B)" */
289. typedef struct {
290. ```
     
     ```

attrFlags_t attrFlags;

attrVals_t attrVals;


292. }gizwitsIssued_t;
293. 
294. /** Corresponding protocol "4.9 Device MCU to the WiFi module to actively report the current state" in the device status "dev_status(11B)" */
295. 
296. typedef struct {
297. uint8_t wBitBuf[COUNT_W_BIT];
298. uint8_t valueTemp;
299. uint8_t valueHumi;
300. uint8_t valueLight_Intensity;
301. } devStatus_t;
302. 
303. 
304. 
305. /** Event queue structure */
306. typedef struct {
307. ```
     
     ```

uint8_t num;                                    ///< Number of queue member

uint8_t event[EVENT_TYPE_MAX]; ///< Queue member event content


309. }eventInfo_t;
310. 
311. 
312. 
313. /** wifiSignal strength structure */
314. typedef struct {
315. ```
     
     ```

uint8_t rssi;                                   ///< WIFI signal strength

  1. }moduleStatusInfo_t;
  2. /** Protocol standard header structure */
  3. typedef struct
  4. {

uint8_t head[2]; ///< The head is 0xFFFF


uint16_t                len;                    ///< From cmd to the end of the entire packet occupied by the number of bytes

uint8_t cmd; ///< command


uint8_t                 sn;                     ///<

uint8_t flags[2]; ///< flag,default is 0


326. } protocolHead_t;
327. 
328. /** 4.1 WiFi module requests the device information protocol structure */
329. typedef struct
330. {
331. ```
     
     ```

protocolHead_t          head;                   ///< Protocol standard header structure

uint8_t protocolVer[8]; ///< Protocol version


uint8_t                 p0Ver[8];               ///< p0 Protocol version

uint8_t hardVer[8]; ///< Hardware version


uint8_t                 softVer[8];             ///< Software version

uint8_t productKey[32]; ///< Product key


uint16_t                ninableTime;            ///< Binding time(second)

uint8_t devAttr[8]; ///< Device attribute


uint8_t                 productSecret[32];      ///< Product secret

uint8_t sum; ///< checksum


341. } protocolDeviceInfo_t;
342. 
343. /** Protocol common data frame(4.2、4.4、4.6、4.9、4.10) protocol structure */
344. typedef struct
345. {
346. ```
     
     ```

protocolHead_t          head;                   ///< Protocol standard header structure

uint8_t sum; ///< checksum


348. } protocolCommon_t;
349. 
350. /** 4.3 The device MCU informs the WiFi module of the configuration mode  protocol structure */
351. typedef struct
352. {
353. ```
     
     ```

protocolHead_t          head;                   ///< Protocol standard header structure

uint8_t cfgMode; ///< Configuration parameters


uint8_t                 sum;                    ///< checksum

  1. } protocolCfgMode_t;
  2. /** 4.13 The MCU requests the network time protocol structure */
  3. typedef struct
  4. {

protocolHead_t head; ///< Protocol standard header structure


uint8_t                 time[7];                ///< Hardware version

uint8_t ntp_time[4]; ///< Software version


uint8_t                 sum;                    ///< checksum

  1. } protocolUTT_t;
  2. /** WiFi module working status*/
  3. typedef union
  4. {

uint16_t value;


struct

{


uint16_t            softap:1;

uint16_t station:1;


uint16_t            onboarding:1;

uint16_t binding:1;


uint16_t            con_route:1;

uint16_t con_m2m:1;


uint16_t            reserve1:2;

uint16_t rssi:3;


uint16_t            app:1;

uint16_t test:1;


uint16_t            reserve2:3;

}types;


385. 
386. } wifiStatus_t;
387. 
388. /** WiFi status type :protocol structure */
389. typedef struct
390. {
391. ```
     
     ```

protocolHead_t          head;                   ///< Protocol standard header structure

wifiStatus_t ststus; ///< WIFI status


uint8_t                 sum;                    ///< checksum

  1. } protocolWifiStatus_t;
  2. /** Protocol common data frame(4.9) :protocol structure */
  3. typedef struct
  4. {

protocolHead_t head; ///< Protocol standard header structure


uint8_t                 type;                   ///< Information Type

uint8_t sum; ///< checksum


402. } protocolGetModuleInfo_t;
403. 
404. typedef struct
405. {
406. ```
     
     ```

uint8_t                 moduleType;             ///< Information Type

uint8_t serialVer[8]; ///< Serial port protocol version


uint8_t                 hardVer[8];             ///< Hardware version

uint8_t softVer[8]; ///< Software version


uint8_t                 mac[16];                ///< mac

uint8_t ip[16]; ///< ip


uint8_t                 devAttr[8];             ///< Device attribute

  1. } moduleInfo_t;
  2. /** Protocol common data frame(4.9) :protocol structure */
  3. typedef struct
  4. {

protocolHead_t head; ///< Protocol standard header structure


moduleInfo_t            wifiModuleInfo;         ///< WIFI module information

uint8_t sum; ///< checksum


421. } protocolModuleInfo_t;
422. 
423. 
424. /** GPRS information of base station */
425. typedef struct
426. {
427. ```
     
     ```

uint16_t                    LAC_ID;             ///<LAC area ID

uint16_t CellID; ///<Base station ID


uint8_t                     RSSI;               ///<Signal strength of base station

  1. } gprsCellInfo_t;
  2. /** 3.19 The basic information of the GPRS communication module */
  3. typedef struct
  4. {

uint8_t Type;//2G/3g/4g


uint8_t                 Pro_ver[8];//Universal serial port protocol version

uint8_t Hard_ver[8];//Hardware version


uint8_t                 Soft_ver[8];//Software version

uint8_t Device_attribute[8];//Device attribute


uint8_t                 IMEI[16];//string

uint8_t IMSI[16];//string


uint8_t                 MCC[8];//Mobile country code

uint8_t MNC[8];//Mobile network code


uint8_t                 CellNum;//Number of base station

uint8_t CellInfoLen;//Information length of base station


gprsCellInfo_t          GPRS_CellINFO[CELLNUMMAX];

  1. }gprsInfo_t;
  2. /** 4.7 Illegal message notification :protocol structure*/
  3. typedef struct
  4. {

protocolHead_t head; ///< Protocol standard header structure


uint8_t                 error;                  ///< error value

uint8_t sum; ///< checksum


456. } protocolErrorType_t;
457. 
458. 
459. /** P0 message header */
460. typedef struct
461. {
462. ```
     
     ```

protocolHead_t          head;                   ///< Protocol standard header structure

uint8_t action; ///< p0 command


464. } protocolP0Head_t;
465. 
466. 
467. /** protocol “4.9 The device MCU reports the current status to the WiFi module” device status "dev_status(11B)"  */
468. typedef struct {
469. 
470. ```
     
     ```

devStatus_t devStatus;                          ///< Stores the device status data

  1. }gizwitsReport_t;
  2. /** resend strategy structure */
  3. typedef struct {

uint8_t num; ///< resend times


uint8_t                 flag;                   ///< 1,Indicates that there is a need to wait for the ACK;0,Indicates that there is no need to wait for the ACK

uint8_t buf[MAX_PACKAGE_LEN]; ///< resend data buffer


uint16_t                dataLen;                ///< resend data length

uint32_t sendTime; ///< resend time


480. } protocolWaitAck_t;
481. 
482. /** 4.8 WiFi read device datapoint value , device ack use this struct */
483. typedef struct
484. {
485. ```
     
     ```

protocolHead_t          head;                   ///< Protocol head

uint8_t action; ///< p0 action


gizwitsReport_t         reportData;             ///< p0 data

uint8_t sum; ///< Checksum


489. } protocolReport_t;
490. 
491. 
492. /** Protocol main and very important struct */
493. typedef struct
494. {
495. ```
     
     ```

uint8_t issuedFlag;                             ///< P0 action type

uint8_t protocolBuf[MAX_PACKAGE_LEN]; ///< Protocol data handle buffer


uint8_t transparentBuff[MAX_PACKAGE_LEN];       ///< Transparent data storage area

uint32_t transparentLen; ///< Transmission data length


uint32_t sn;                                    ///< Message SN

uint32_t timerMsCount; ///< Timer Count


protocolWaitAck_t waitAck;                      ///< Protocol wait ACK data structure

eventInfo_t issuedProcessEvent; ///< Control events


eventInfo_t wifiStatusEvent;                    ///< WIFI Status events

eventInfo_t NTPEvent; ///< NTP events


eventInfo_t moduleInfoEvent;                    ///< Module Info events

gizwitsReport_t reportData; ///< The protocol reports data for standard product


dataPoint_t gizCurrentDataPoint;                ///< Current device datapoints status

dataPoint_t gizLastDataPoint; ///< Last device datapoints status


moduleStatusInfo_t wifiStatusData;              ///< WIFI signal intensity

protocolTime_t TimeNTP; ///< Network time information


514. #if MODULE_TYPE
515. ```
     
     ```

gprsInfo_t   gprsInfoNews;

  1. #else

moduleInfo_t wifiModuleNews; ///< WIFI module Info


518. #endif
519. 
520. 
521. }gizwitsProtocol_t;
522. 
523. #pragma pack()
524. 
525. /**@name Gizwits user API interface
526. * @{
527. */
528. 
529. extern uint32_t gizGetTimerCount(void);
530. 
531. void gizwitsInit(void);
532. int32_t gizwitsSetMode(uint8_t mode);
533. void gizwitsGetNTP(void);
534. int32_t gizwitsHandle(dataPoint_t *currentData);
535. int32_t gizwitsPassthroughData(uint8_t * gizdata, uint32_t len);
536. void gizwitsGetModuleInfo(void);
537. int32_t gizPutData(uint8_t *buf, uint32_t len);
538. 
539. 
540. / *添加用户自定义的函数* */
541. void gziwits_Task(dataPoint_t * currentDataPoint);
542. 
543. /**@} */
544. #ifdef __cplusplus
545. }
546. #endif
547. 
548. #endif
549. 
550. 

*复制代码*

6,Utils直接移植无需修改

![](//file.elecfans.com/web2/M00/55/A7/poYBAGLeVoiAadhxAADUD1BG6KU957.jpg)

7,添加TIM3定时器代码驱动

1. #include "tim3.h"
2. #include "gizwits_product.h"
3. 
4. void TIM3_Config(uint16_t psc,uint16_t arr)
5. {
6. ```
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  1. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    
  2. TIM_TimeBaseStructure.TIM_Period                     = arr;
    
  3. 
    

TIM_TimeBaseStructure.TIM_Prescaler                  = psc;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;


TIM_TimeBaseStructure.TIM_CounterMode                = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );

  1. NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;


NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;


NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;

NVIC_Init(&NVIC_InitStructure);


TIM_Cmd(TIM3, ENABLE);

  1. }
  2. /用户实现的定时器接口/
  3. void TIM3_IRQHandler(void)
  4. {

if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)


{

TIM_ClearITPendingBit(TIM3, TIM_IT_Update );


gizTimerMs();

}


33. }
34. /*******************/
35. //void TIMER_IRQ_FUN(void)
36. //{
37. //  gizTimerMs();
38. //}
39. /*******************/
40. 
41. 
42. ## 添加UART3串口通信代码驱动
43. 
44. ```c
    
    ```

  1. #include"usart3.h"
  2. #include"gizwits_product.h"
  3. voidUSART3_Init(uint32_t BaudRate)
  4. {

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //GPIOB时钟


RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);                          //串口3时钟使能

USART_DeInit(USART3); //复位串口3


GPIO_InitTypeDef GPIO_InitStructure;

  1. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //USART3_TX PB10


GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                                       //复用推挽输出

GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB10


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;                                              //USART3_RX          PB11

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入


GPIO_Init(GPIOB, &GPIO_InitStructure);

USART_InitTypeDef USART_InitStructure;


USART_InitStructure.USART_BaudRate            = BaudRate;                                    //波特率一般设置为9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式


USART_InitStructure.USART_StopBits            = USART_StopBits_1;                         //一个停止位

USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位


USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式


USART_Init(USART3, &USART_InitStructure);       //初始化串口3

USART_Cmd(USART3, ENABLE); //使能串口


79. 
80. 
81. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);       //开启中断
82. 
83. NVIC_InitTypeDef NVIC_InitStructure;
84. ```
    
    ```

NVIC_InitStructure.NVIC_IRQChannel                   = USART3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;


NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 3;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;


NVIC_Init(&NVIC_InitStructure);

  1. }
  2. /用户实现的中断服务函数接口/
  3. void USART3_IRQHandler(void)
  4. {

uint8_t Recv_Data;


if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//接收到数据

{


Recv_Data = USART_ReceiveData(USART3);

gizPutData(&Recv_Data, 1);


}

  1. }
  2. /*********************/
  3. //void UART_IRQ_FUN(void)
  4. //{
  5. // uint8_t value = 0;
  6. // gizPutData(&value, 1);
  7. //}
  8. /*********************/

复制代码

8,修改关键函数的函数体,封装各模块的初始化

五,机智云初始化函数封装
成功联网

设备上线显示

效果展示


更多回帖

发帖
×
20
完善资料,
赚取积分