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

第一篇内容:总体设计/系统功能介绍/机智云自助开发平台-开发利器GAgent等等
点击下载:【Io开发笔记】机智云智能浇花器实战(1)-基础Demo实现

第二篇内容:
继电器实现/功能测试/DHT11驱动代码实现/OLED屏幕显示传感器数据/中文字模制作等等
点击下载:机智云智能浇花器实战(2)-基础Demo实现


一,BH1750光照传感器原理图



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



二,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; //拉低数据线
  19. BH1750_SCL_H; //拉高时钟线
  20. Delay_nus(5); //延时
  21. GPIO_SetBits(BH1750_PORT, BH1750_SDA_PIN); //产生上升沿
  22. Delay_nus(5); //延时
  23. }

  24. /******************
  25. 发送应答信号
  26. 入口参数:ack (0:ACK 1:NAK)
  27. ******************/
  28. void BH1750_SendACK(int ack)
  29. {
  30. GPIO_InitTypeDef GPIO_InitStruct;

  31. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  32. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  33. GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
  34. GPIO_Init(BH1750_PORT, &GPIO_InitStruct);

  35. if(ack == 1) //写应答信号
  36. BH1750_SDA_H;
  37. else if(ack == 0)
  38. BH1750_SDA_L;
  39. else
  40. return;
  41. BH1750_SCL_H; //拉高时钟线
  42. Delay_nus(5); //延时
  43. BH1750_SCL_L; //拉低时钟线
  44. Delay_nus(5); //延时
  45. }

  46. /******************
  47. 接收应答信号
  48. ******************/
  49. int BH1750_RecvACK()
  50. {
  51. GPIO_InitTypeDef GPIO_InitStruct;

  52. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; /*这里一定要设成输入上拉,否则不能读出数据*/
  53. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  54. GPIO_InitStruct.GPIO_Pin=BH1750_SDA_PIN;
  55. GPIO_Init(BH1750_PORT,&GPIO_InitStruct);

  56. BH1750_SCL_H; //拉高时钟线
  57. Delay_nus(5); //延时
  58. if(GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN)==1)//读应答信号
  59. mcy = 1 ;
  60. else
  61. mcy = 0 ;
  62. BH1750_SCL_L; //拉低时钟线
  63. Delay_nus(5); //延时
  64. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
  65. GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
  66. return mcy;
  67. }

  68. /******************
  69. 向IIC总线发送一个字节数据
  70. ******************/
  71. void BH1750_SendByte(uint8_t dat)
  72. {
  73. uint8_t i;
  74. for (i=0; i<8; i++) //8位计数器
  75. {
  76. if( 0X80 & dat )
  77. GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN);
  78. else
  79. GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN);
  80. dat <<= 1;
  81. BH1750_SCL_H; //拉高时钟线
  82. Delay_nus(5); //延时
  83. BH1750_SCL_L; //拉低时钟线
  84. Delay_nus(5); //延时
  85. }
  86. BH1750_RecvACK();
  87. }

  88. uint8_t BH1750_RecvByte()
  89. {
  90. uint8_t i;
  91. uint8_t dat = 0;
  92. uint8_t bit;

  93. GPIO_InitTypeDef GPIO_InitStruct;

  94. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; /*这里一定要设成输入上拉,否则不能读出数据*/
  95. GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
  96. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  97. GPIO_Init(BH1750_PORT,&GPIO_InitStruct );

  98. GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN); //使能内部上拉,准备读取数据,
  99. for (i=0; i<8; i++) //8位计数器
  100. {
  101. dat <<= 1;
  102. BH1750_SCL_H; //拉高时钟线
  103. Delay_nus(5); //延时

  104. if( SET == GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN))
  105. bit = 0X01;
  106. else
  107. bit = 0x00;
  108. dat |= bit; //读数据
  109. BH1750_SCL_L; //拉低时钟线
  110. Delay_nus(5); //延时
  111. }
  112. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  113. GPIO_Init(BH1750_PORT, &GPIO_InitStruct );
  114. return dat;
  115. }

  116. void Single_Write_BH1750(uint8_t REG_Address)
  117. {
  118. BH1750_Start(); //起始信号
  119. BH1750_SendByte(SlaveAddress); //发送设备地址+写信号
  120. BH1750_SendByte(REG_Address); //内部寄存器地址,请参考中文pdf22页
  121. // BH1750_SendByte(REG_data); //内部寄存器数据,请参考中文pdf22页
  122. BH1750_Stop(); //发送停止信号
  123. }

  124. //初始化BH1750,根据需要请参考pdf进行修改**
  125. void BH1750_Init()
  126. {
  127. GPIO_InitTypeDef GPIO_InitStruct;
  128. /*开启GPIOB的外设时钟*/
  129. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
  130. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  131. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  132. GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN | BH1750_SCL_PIN ;
  133. GPIO_Init(BH1750_PORT,&GPIO_InitStruct);

  134. Single_Write_BH1750(0x01);
  135. Delay_nms(180); //延时180ms
  136. }

  137. //连续读出BH1750内部数据
  138. void mread(void)
  139. {
  140. uint8_t i;
  141. BH1750_Start(); //起始信号
  142. BH1750_SendByte(SlaveAddress+1); //发送设备地址+读信号

  143. for (i=0; i<3; i++) //连续读取6个地址数据,存储中BUF
  144. {
  145. BUF[i] = BH1750_RecvByte(); //BUF[0]存储0x32地址中的数据
  146. if (i == 3)
  147. {
  148. BH1750_SendACK(1); //最后一个数据需要回NOACK
  149. }
  150. else
  151. {
  152. BH1750_SendACK(0); //回应ACK
  153. }
  154. }
  155. BH1750_Stop(); //停止信号
  156. Delay_nms(5);
  157. }

  158. float Read_BH1750(void)
  159. {
  160. int dis_data; //变量
  161. float temp1;
  162. float temp2;
  163. Single_Write_BH1750(0x01); // power on
  164. Single_Write_BH1750(0x10); // H- resolution mode
  165. Delay_nms(180); //延时180ms
  166. mread(); //连续读出数据,存储在BUF中
  167. dis_data=BUF[0];
  168. dis_data=(dis_data<<8)+BUF[1]; //合成数据
  169. temp1=dis_data/1.2;
  170. temp2=10*dis_data/1.2;
  171. temp2=(int)temp2%10;
  172. return temp1;
  173. }


复制代码



  1. #ifndef __BH1750_H__
  2. #define __BH1750_H__

  3. #include "STM32f10x.h"

  4. /*
  5. 定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改
  6. ALT ADDRESS引脚接地时地址为0x46, 接电源时地址为0xB8
  7. */
  8. #define SlaveAddress 0x46
  9. #define BH1750_PORT GPIOB
  10. #define BH1750_SCL_PIN GPIO_Pin_1
  11. #define BH1750_SDA_PIN GPIO_Pin_0

  12. #define BH1750_SCL_H GPIO_SetBits(BH1750_PORT,BH1750_SCL_PIN)
  13. #define BH1750_SCL_L GPIO_ResetBits(BH1750_PORT,BH1750_SCL_PIN)

  14. #define BH1750_SDA_H GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN)
  15. #define BH1750_SDA_L GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN)



  16. extern uint8_t BUF[8]; //接收数据缓存区
  17. extern int dis_data; //变量
  18. extern int mcy; //表示进位标志位

  19. void BH1750_Init(void);
  20. void conversion(uint32_t temp_data);
  21. void Single_Write_BH1750(uint8_t REG_Address); //单个写入数据
  22. uint8_t Single_Read_BH1750(uint8_t REG_Address); //单个读取内部寄存器数据
  23. void mread(void); //连续的读取内部寄存器数据
  24. float Read_BH1750(void);


  25. #endif



复制代码


BH1750传感器代码说明

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



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



三,机智云自助开发平台数据点创建
机智云官方网站:https://www.gizwits.com/
步骤1,创建产品


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



创建好后就会有基本信息



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



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

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



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


  1. Product Key :9c8a5a8e38344fb4af14b6db0f5b1df7

  2. Product Secret :45c86d8c6a2a4b1dac7d68df54f6e4f0


复制代码



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

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



只读:就是只允许赋值数据传感器赋值给平台平台只能读取
可写:就是数据可以被修改继电器的开关状态平台可以修改

四,MCU开发

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



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

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




1,生成代码包

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



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

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


3,机智云Gizwits协议移植

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



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



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



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


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

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


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

4,修改gizwits_product.c


  1. #include
  2. #include
  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;
  24. }

  25. for(i=0; inum; i++)
  26. {
  27. switch(info->event[i])
  28. {
  29. case EVENT_Relay_1:
  30. currentDataPoint.valueRelay_1 = dataPointPtr->valueRelay_1;
  31. GIZWITS_LOG("Evt: EVENT_Relay_1 %d ", currentDataPoint.valueRelay_1);
  32. if(0x01 == currentDataPoint.valueRelay_1)
  33. {
  34. currentDataPoint.valueRelay_1 = 1;

  35. }
  36. else
  37. {
  38. currentDataPoint.valueRelay_1 = 0;

  39. }
  40. break;
  41. case WIFI_SOFTAP:
  42. break;
  43. case WIFI_AIRLINK:
  44. break;
  45. case WIFI_STATION:
  46. break;
  47. case WIFI_CON_ROUTER:

  48. break;
  49. case WIFI_DISCON_ROUTER:

  50. break;
  51. case WIFI_CON_M2M:
  52. wifi_flag = 1; //WiFi连接标志
  53. break;
  54. case WIFI_DISCON_M2M:
  55. wifi_flag = 0; //WiFi断开标志
  56. break;
  57. case WIFI_RSSI:
  58. GIZWITS_LOG("RSSI %d ", wifiData->rssi);
  59. break;
  60. case TRANSPARENT_DATA:
  61. GIZWITS_LOG("TRANSPARENT_DATA ");
  62. //user handle , Fetch data from [data] , size is [len]
  63. break;
  64. case WIFI_NTP:
  65. 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);
  66. break;
  67. case MODULE_INFO:
  68. GIZWITS_LOG("MODULE INFO ... ");
  69. #if MODULE_TYPE
  70. GIZWITS_LOG("GPRS MODULE ... ");
  71. //Format By gprsInfo_t
  72. #else
  73. GIZWITS_LOG("WIF MODULE ... ");
  74. //Format By moduleInfo_t
  75. GIZWITS_LOG("moduleType : [%d] ",ptModuleInfo->moduleType);
  76. #endif
  77. break;
  78. default:
  79. break;
  80. }
  81. }

  82. return 0;
  83. }


  84. void userHandle(void)
  85. {
  86. /*
  87. currentDataPoint.valueTemp = ;//Add Sensor Data Collection
  88. currentDataPoint.valueHumi = ;//Add Sensor Data Collection
  89. currentDataPoint.valueLight_Intensity = ;//Add Sensor Data Collection
  90. */
  91. }
  92. void userInit(void)
  93. {
  94. memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));
  95. currentDataPoint.valueRelay_1 = 0;
  96. currentDataPoint.valueTemp = 0;
  97. currentDataPoint.valueHumi = 0;
  98. currentDataPoint.valueLight_Intensity = 0;
  99. }

  100. void gizTimerMs(void)
  101. {
  102. timerMsCount++;
  103. }

  104. uint32_t gizGetTimerCount(void)
  105. {
  106. return timerMsCount;
  107. }

  108. void mcuRestart(void)
  109. {
  110. __set_FAULTMASK(1);
  111. NVIC_SystemReset();
  112. }

  113. void TIMER_IRQ_FUN(void)
  114. {
  115. gizTimerMs();
  116. }

  117. void UART_IRQ_FUN(void)
  118. {
  119. uint8_t value = 0;
  120. gizPutData(&value, 1);
  121. }

  122. int32_t uartWrite(uint8_t *buf, uint32_t len)
  123. {
  124. uint32_t i = 0;

  125. if(NULL == buf)
  126. {
  127. return -1;
  128. }

  129. for(i=0; i
  130. {
  131. USART_SendData(USART3,buf[i]);
  132. while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //循环发送,直到发送完毕
  133. if(i >=2 && buf[i] == 0xFF)
  134. {
  135. USART_SendData(USART3, 0x55);
  136. while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //循环发送,直到发送完毕
  137. }
  138. }
  139. return len;
  140. }


复制代码

5,修改

  1. #ifndef _GIZWITS_PRODUCT_H
  2. #define _GIZWITS_PRODUCT_H

  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif

  6. #include
  7. //#include "stm32f1xx.h"
  8. #include "gizwits_protocol.h"

  9. /**
  10. * MCU software version
  11. */
  12. #define SOFTWARE_VERSION "03030000"
  13. /**
  14. * MCU hardware version
  15. */
  16. #define HARDWARE_VERSION "03010100"


  17. /**
  18. * Communication module model
  19. */
  20. #define MODULE_TYPE 0 //0,WIFI ;1,GPRS


  21. /**@name TIM3 related macro definition
  22. * @{
  23. */
  24. #define TIMER TIM3
  25. #define TIMER_IRQ TIM3_IRQn
  26. #define TIMER_RCC RCC_APB1Periph_TIM3
  27. #define TIMER_IRQ_FUN TIM3_IRQHandler
  28. /**@} */

  29. /**@name USART related macro definition
  30. * @{
  31. */
  32. #define UART_BAUDRATE 9600
  33. #define UART_PORT 2
  34. #define UART USART2
  35. #define UART_IRQ USART2_IRQn
  36. #define UART_IRQ_FUN USART2_IRQHandler

  37. #if (UART_PORT == 1)
  38. #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
  39. #define UART_GPIO_CLK RCC_APB2Periph_GPIOA

  40. #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
  41. #define UART_AFIO_CLK RCC_APB2Periph_AFIO

  42. #define UART_CLK_Cmd RCC_APB2PeriphClockCmd
  43. #define UART_CLK RCC_APB2Periph_USART1

  44. #define UART_GPIO_PORT GPIOA
  45. #define UART_RxPin GPIO_Pin_10
  46. #define UART_TxPin GPIO_Pin_9
  47. #endif

  48. #if (UART_PORT == 2)
  49. #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
  50. #define UART_GPIO_CLK RCC_APB2Periph_GPIOA

  51. #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
  52. #define UART_AFIO_CLK RCC_APB2Periph_AFIO

  53. #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
  54. #define UART_CLK RCC_APB1Periph_USART2

  55. #define UART_GPIO_PORT GPIOA
  56. #define UART_RxPin GPIO_Pin_3
  57. #define UART_TxPin GPIO_Pin_2
  58. #endif


  59. #if (UART_PORT == 3)

  60. #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
  61. #define UART_GPIO_CLK RCC_APB2Periph_GPIOC

  62. #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
  63. #define UART_AFIO_CLK RCC_APB2Periph_AFIO

  64. #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
  65. #define UART_CLK RCC_APB1Periph_USART3

  66. #define UART_GPIO_PORT GPIOC
  67. #define UART_RxPin GPIO_Pin_11
  68. #define UART_TxPin GPIO_Pin_10

  69. #endif
  70. /**@} */

  71. /** User area the current device state structure*/
  72. extern dataPoint_t currentDataPoint;

  73. void gizTimerMs(void);
  74. uint32_t gizGetTimerCount(void);
  75. void timerInit(void);
  76. void uartInit(void);

  77. void userInit(void);
  78. void userHandle(void);
  79. void mcuRestart(void);

  80. int32_t uartWrite(uint8_t *buf, uint32_t len);
  81. int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len);

  82. #ifdef __cplusplus
  83. }
  84. #endif

  85. #endif


复制代码


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

  1. /**
  2. ***************************
  3. * @file gizwits_protocol.c
  4. * @brief Corresponding gizwits_product.c header file (including product hardware and software version definition)
  5. * @author Gizwits
  6. * @date 2017-07-19
  7. * @version V03030000
  8. * @copyright Gizwits
  9. *
  10. * @note 机智云.只为智能硬件而生
  11. * Gizwits Smart Cloud for Smart Products
  12. * 链接|增值ֵ|开放|中立|安全|自有|自由|生态
  13. * 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. /**@name The serial port receives the ring buffer implementation
  25. * @{
  26. */
  27. rb_t pRb; ///< Ring buffer structure variable
  28. static uint8_t rbBuf[RB_MAX_LEN]; ///< Ring buffer data cache buffer


  29. /**@} */

  30. /**
  31. * @brief Write data to the ring buffer
  32. * @param [in] buf : buf adress
  33. * @param [in] len : byte length
  34. * @return correct : Returns the length of the written data
  35. failure : -1
  36. */
  37. int32_t gizPutData(uint8_t *buf, uint32_t len)
  38. {
  39. int32_t count = 0;

  40. if(NULL == buf)
  41. {
  42. GIZWITS_LOG("ERR: gizPutData buf is empty ");
  43. return -1;
  44. }

  45. count = rbWrite(&pRb, buf, len);
  46. if(count != len)
  47. {
  48. GIZWITS_LOG("ERR: Failed to rbWrite ");
  49. return -1;
  50. }

  51. return count;
  52. }



  53. /**
  54. * @brief Protocol header initialization
  55. *
  56. * @param [out] head : Protocol header pointer
  57. *
  58. * @return 0, success; other, failure
  59. */
  60. static int8_t gizProtocolHeadInit(protocolHead_t *head)
  61. {
  62. if(NULL == head)
  63. {
  64. GIZWITS_LOG("ERR: gizProtocolHeadInit head is empty ");
  65. return -1;
  66. }

  67. memset((uint8_t *)head, 0, sizeof(protocolHead_t));
  68. head->head[0] = 0xFF;
  69. head->head[1] = 0xFF;

  70. return 0;
  71. }

  72. /**
  73. * @brief Protocol ACK check processing function
  74. *
  75. * @param [in] data : data adress
  76. * @param [in] len : data length
  77. *
  78. * @return 0, suceess; other, failure
  79. */
  80. static int8_t gizProtocolWaitAck(uint8_t *gizdata, uint32_t len)
  81. {
  82. if(NULL == gizdata)
  83. {
  84. GIZWITS_LOG("ERR: data is empty ");
  85. return -1;
  86. }

  87. memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));
  88. memcpy((uint8_t *)gizwitsProtocol.waitAck.buf, gizdata, len);
  89. gizwitsProtocol.waitAck.dataLen = (uint16_t)len;

  90. gizwitsProtocol.waitAck.flag = 1;
  91. gizwitsProtocol.waitAck.sendTime = gizGetTimerCount();

  92. return 0;
  93. }
  94. /**
  95. * @brief generates "controlled events" according to protocol

  96. * @param [in] issuedData: Controlled data
  97. * @param [out] info: event queue
  98. * @param [out] dataPoints: data point data
  99. * @return 0, the implementation of success, non-0, failed
  100. */
  101. static int8_t ICACHE_FLASH_ATTR gizDataPoint2Event(gizwitsIssued_t *issuedData, eventInfo_t *info, dataPoint_t *dataPoints)
  102. {
  103. if((NULL == issuedData) || (NULL == info) ||(NULL == dataPoints))
  104. {
  105. GIZWITS_LOG("gizDataPoint2Event Error , Illegal Param ");
  106. return -1;
  107. }

  108. /** Greater than 1 byte to do bit conversion **/
  109. if(sizeof(issuedData->attrFlags) > 1)
  110. {
  111. if(-1 == gizByteOrderExchange((uint8_t *)&issuedData->attrFlags,sizeof(attrFlags_t)))
  112. {
  113. GIZWITS_LOG("gizByteOrderExchange Error ");
  114. return -1;
  115. }
  116. }

  117. if(0x01 == issuedData->attrFlags.flagRelay_1)
  118. {
  119. info->event[info->num] = EVENT_Relay_1;
  120. info->num++;
  121. dataPoints->valueRelay_1 = gizStandardDecompressionValue(Relay_1_BYTEOFFSET,Relay_1_BITOFFSET,Relay_1_LEN,(uint8_t *)&issuedData->attrVals.wBitBuf,sizeof(issuedData->attrVals.wBitBuf));
  122. }


  123. return 0;
  124. }

  125. /**
  126. * @brief contrasts the current data with the last data
  127. *
  128. * @param [in] cur: current data point data
  129. * @param [in] last: last data point data
  130. *
  131. * @return: 0, no change in data; 1, data changes
  132. */
  133. static int8_t ICACHE_FLASH_ATTR gizCheckReport(dataPoint_t *cur, dataPoint_t *last)
  134. {
  135. int8_t ret = 0;
  136. static uint32_t lastReportTime = 0;
  137. uint32_t currentTime = 0;

  138. if((NULL == cur) || (NULL == last))
  139. {
  140. GIZWITS_LOG("gizCheckReport Error , Illegal Param ");
  141. return -1;
  142. }
  143. currentTime = gizGetTimerCount();
  144. if(last->valueRelay_1 != cur->valueRelay_1)
  145. {
  146. GIZWITS_LOG("valueRelay_1 Changed ");
  147. ret = 1;
  148. }

  149. if(last->valueTemp != cur->valueTemp)
  150. {
  151. if(currentTime - lastReportTime >= REPORT_TIME_MAX)
  152. {
  153. GIZWITS_LOG("valueTemp Changed ");
  154. ret = 1;
  155. }
  156. }
  157. if(last->valueHumi != cur->valueHumi)
  158. {
  159. if(currentTime - lastReportTime >= REPORT_TIME_MAX)
  160. {
  161. GIZWITS_LOG("valueHumi Changed ");
  162. ret = 1;
  163. }
  164. }
  165. if(last->valueLight_Intensity != cur->valueLight_Intensity)
  166. {
  167. if(currentTime - lastReportTime >= REPORT_TIME_MAX)
  168. {
  169. GIZWITS_LOG("valueLight_Intensity Changed ");
  170. ret = 1;
  171. }
  172. }

  173. if(1 == ret)
  174. {
  175. lastReportTime = gizGetTimerCount();
  176. }
  177. return ret;
  178. }

  179. /**
  180. * @brief User data point data is converted to wit the cloud to report data point data
  181. *
  182. * @param [in] dataPoints: user data point data address
  183. * @param [out] devStatusPtr: wit the cloud data point data address
  184. *
  185. * @return 0, the correct return; -1, the error returned
  186. */
  187. static int8_t ICACHE_FLASH_ATTR gizDataPoints2ReportData(dataPoint_t *dataPoints , devStatus_t *devStatusPtr)
  188. {
  189. if((NULL == dataPoints) || (NULL == devStatusPtr))
  190. {
  191. GIZWITS_LOG("gizDataPoints2ReportData Error , Illegal Param ");
  192. return -1;
  193. }

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

  195. gizStandardCompressValue(Relay_1_BYTEOFFSET,Relay_1_BITOFFSET,Relay_1_LEN,(uint8_t *)devStatusPtr,dataPoints->valueRelay_1);
  196. gizByteOrderExchange((uint8_t *)devStatusPtr->wBitBuf,sizeof(devStatusPtr->wBitBuf));

  197. devStatusPtr->valueTemp = gizY2X(Temp_RATIO, Temp_ADDITION, dataPoints->valueTemp);
  198. devStatusPtr->valueHumi = gizY2X(Humi_RATIO, Humi_ADDITION, dataPoints->valueHumi);
  199. devStatusPtr->valueLight_Intensity = gizY2X(Light_Intensity_RATIO, Light_Intensity_ADDITION, dataPoints->valueLight_Intensity);




  200. return 0;
  201. }


  202. /**
  203. * @brief This function is called by the GAgent module to receive the relevant protocol data from the cloud or APP
  204. * @param [in] inData The protocol data entered
  205. * @param [in] inLen Enter the length of the data
  206. * @param [out] outData The output of the protocol data
  207. * @param [out] outLen The length of the output data
  208. * @return 0, the implementation of success, non-0, failed
  209. */
  210. static int8_t gizProtocolIssuedProcess(char *did, uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t *outLen)
  211. {
  212. uint8_t issuedAction = inData[0];

  213. if((NULL == inData)||(NULL == outData)||(NULL == outLen))
  214. {
  215. GIZWITS_LOG("gizProtocolIssuedProcess Error , Illegal Param ");
  216. return -1;
  217. }

  218. if(NULL == did)
  219. {
  220. memset((uint8_t *)&gizwitsProtocol.issuedProcessEvent, 0, sizeof(eventInfo_t));
  221. switch(issuedAction)
  222. {
  223. case ACTION_CONTROL_DEVICE:
  224. gizDataPoint2Event((gizwitsIssued_t *)&inData[1], &gizwitsProtocol.issuedProcessEvent,&gizwitsProtocol.gizCurrentDataPoint);
  225. gizwitsProtocol.issuedFlag = ACTION_CONTROL_TYPE;
  226. outData = NULL;
  227. *outLen = 0;
  228. break;

  229. case ACTION_READ_DEV_STATUS:
  230. if(0 == gizDataPoints2ReportData(&gizwitsProtocol.gizLastDataPoint,&gizwitsProtocol.reportData.devStatus))
  231. {
  232. memcpy(outData+1, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(gizwitsReport_t));
  233. outData[0] = ACTION_READ_DEV_STATUS_ACK;
  234. *outLen = sizeof(gizwitsReport_t)+1;
  235. }
  236. else
  237. {
  238. return -1;
  239. }
  240. break;
  241. case ACTION_W2D_TRANSPARENT_DATA:
  242. memcpy(gizwitsProtocol.transparentBuff, &inData[1], inLen-1);
  243. gizwitsProtocol.transparentLen = inLen - 1;

  244. gizwitsProtocol.issuedProcessEvent.event[gizwitsProtocol.issuedProcessEvent.num] = TRANSPARENT_DATA;
  245. gizwitsProtocol.issuedProcessEvent.num++;
  246. gizwitsProtocol.issuedFlag = ACTION_W2D_TRANSPARENT_TYPE;
  247. outData = NULL;
  248. *outLen = 0;
  249. break;

  250. default:
  251. break;
  252. }
  253. }

  254. return 0;
  255. }
  256. /**
  257. * @brief The protocol sends data back , P0 ACK
  258. *
  259. * @param [in] head : Protocol head pointer
  260. * @param [in] data : Payload data
  261. * @param [in] len : Payload data length
  262. * @param [in] proFlag : DID flag ,1 for Virtual sub device did ,0 for single product or gateway
  263. *
  264. * @return : 0,Ack success;
  265. * -1,Input Param Illegal
  266. * -2,Serial send faild
  267. */
  268. static int32_t gizProtocolIssuedDataAck(protocolHead_t *head, uint8_t *gizdata, uint32_t len, uint8_t proFlag)
  269. {
  270. int32_t ret = 0;
  271. uint8_t tx_buf[RB_MAX_LEN];
  272. uint32_t offset = 0;
  273. uint8_t sDidLen = 0;
  274. uint16_t data_len = 0;
  275. uint8_t *pTxBuf = tx_buf;
  276. if(NULL == gizdata)
  277. {
  278. GIZWITS_LOG("[ERR] data Is Null ");
  279. return -1;
  280. }


  281. if(0x1 == proFlag)
  282. {
  283. sDidLen = *((uint8_t *)head + sizeof(protocolHead_t));
  284. data_len = 5 + 1 + sDidLen + len;
  285. }
  286. else
  287. {
  288. data_len = 5 + len;
  289. }
  290. GIZWITS_LOG("len = %d , sDidLen = %d ,data_len = %d ", len,sDidLen,data_len);
  291. *pTxBuf ++= 0xFF;
  292. *pTxBuf ++= 0xFF;
  293. *pTxBuf ++= (uint8_t)(data_len>>8);
  294. *pTxBuf ++= (uint8_t)(data_len);
  295. *pTxBuf ++= head->cmd + 1;
  296. *pTxBuf ++= head->sn;
  297. *pTxBuf ++= 0x00;
  298. *pTxBuf ++= proFlag;
  299. offset = 8;
  300. if(0x1 == proFlag)
  301. {
  302. *pTxBuf ++= sDidLen;
  303. offset += 1;
  304. memcpy(&tx_buf[offset],(uint8_t *)head+sizeof(protocolHead_t)+1,sDidLen);
  305. offset += sDidLen;
  306. pTxBuf += sDidLen;

  307. }
  308. if(0 != len)
  309. {
  310. memcpy(&tx_buf[offset],gizdata,len);
  311. }
  312. tx_buf[data_len + 4 - 1 ] = gizProtocolSum( tx_buf , (data_len+4));

  313. ret = uartWrite(tx_buf, data_len+4);
  314. if(ret < 0)
  315. {
  316. GIZWITS_LOG("uart write error %d ", ret);
  317. return -2;
  318. }

  319. return 0;
  320. }

  321. /**
  322. * @brief Report data interface
  323. *
  324. * @param [in] action : PO action
  325. * @param [in] data : Payload data
  326. * @param [in] len : Payload data length
  327. *
  328. * @return : 0,Ack success;
  329. * -1,Input Param Illegal
  330. * -2,Serial send faild
  331. */
  332. static int32_t gizReportData(uint8_t action, uint8_t *gizdata, uint32_t len)
  333. {
  334. int32_t ret = 0;
  335. protocolReport_t protocolReport;

  336. if(NULL == gizdata)
  337. {
  338. GIZWITS_LOG("gizReportData Error , Illegal Param ");
  339. return -1;
  340. }
  341. gizProtocolHeadInit((protocolHead_t *)&protocolReport);
  342. protocolReport.head.cmd = CMD_REPORT_P0;
  343. protocolReport.head.sn = gizwitsProtocol.sn++;
  344. protocolReport.action = action;
  345. protocolReport.head.len = exchangeBytes(sizeof(protocolReport_t)-4);
  346. memcpy((gizwitsReport_t *)&protocolReport.reportData, (gizwitsReport_t *)gizdata,len);
  347. protocolReport.sum = gizProtocolSum((uint8_t *)&protocolReport, sizeof(protocolReport_t));

  348. ret = uartWrite((uint8_t *)&protocolReport, sizeof(protocolReport_t));
  349. if(ret < 0)
  350. {
  351. GIZWITS_LOG("ERR: uart write error %d ", ret);
  352. return -2;
  353. }

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

  355. return ret;
  356. }/**
  357. * @brief Datapoints reporting mechanism
  358. *
  359. * 1. Changes are reported immediately

  360. * 2. Data timing report , 600000 Millisecond
  361. *
  362. *@param [in] currentData : Current datapoints value
  363. * @return : NULL
  364. */
  365. static void gizDevReportPolicy(dataPoint_t *currentData)
  366. {
  367. static uint32_t lastRepTime = 0;
  368. uint32_t timeNow = gizGetTimerCount();

  369. if((1 == gizCheckReport(currentData, (dataPoint_t *)&gizwitsProtocol.gizLastDataPoint)))
  370. {
  371. GIZWITS_LOG("changed, report data ");
  372. if(0 == gizDataPoints2ReportData(currentData,&gizwitsProtocol.reportData.devStatus))
  373. {
  374. gizReportData(ACTION_REPORT_DEV_STATUS, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(devStatus_t)); }
  375. memcpy((uint8_t *)&gizwitsProtocol.gizLastDataPoint, (uint8_t *)currentData, sizeof(dataPoint_t));
  376. }

  377. if((0 == (timeNow % (600000))) && (lastRepTime != timeNow))
  378. {
  379. GIZWITS_LOG("Info: 600S report data ");
  380. if(0 == gizDataPoints2ReportData(currentData,&gizwitsProtocol.reportData.devStatus))
  381. {
  382. gizReportData(ACTION_REPORT_DEV_STATUS, (uint8_t *)&gizwitsProtocol.reportData.devStatus, sizeof(devStatus_t));
  383. }
  384. memcpy((uint8_t *)&gizwitsProtocol.gizLastDataPoint, (uint8_t *)currentData, sizeof(dataPoint_t));

  385. lastRepTime = timeNow;
  386. }
  387. }

  388. /**
  389. * @brief Get a packet of data from the ring buffer
  390. *
  391. * @param [in] rb : Input data address
  392. * @param [out] data : Output data address
  393. * @param [out] len : Output data length
  394. *
  395. * @return : 0,Return correct ;-1,Return failure;-2,Data check failure
  396. */
  397. static int8_t gizProtocolGetOnePacket(rb_t *rb, uint8_t *gizdata, uint16_t *len)
  398. {
  399. int32_t ret = 0;
  400. uint8_t sum = 0;
  401. int32_t i = 0;
  402. uint8_t tmpData;
  403. uint8_t tmpLen = 0;
  404. uint16_t tmpCount = 0;
  405. static uint8_t protocolFlag = 0;
  406. static uint16_t protocolCount = 0;
  407. static uint8_t lastData = 0;
  408. static uint8_t debugCount = 0;
  409. uint8_t *protocolBuff = gizdata;
  410. protocolHead_t *head = NULL;

  411. if((NULL == rb) || (NULL == gizdata) ||(NULL == len))
  412. {
  413. GIZWITS_LOG("gizProtocolGetOnePacket Error , Illegal Param ");
  414. return -1;
  415. }

  416. tmpLen = rbCanRead(rb);
  417. if(0 == tmpLen)
  418. {
  419. return -1;
  420. }

  421. for(i=0; i
  422. {
  423. ret = rbRead(rb, &tmpData, 1);
  424. if(0 != ret)
  425. {
  426. if((0xFF == lastData) && (0xFF == tmpData))
  427. {
  428. if(0 == protocolFlag)
  429. {
  430. protocolBuff[0] = 0xFF;
  431. protocolBuff[1] = 0xFF;
  432. protocolCount = 2;
  433. protocolFlag = 1;
  434. }
  435. else
  436. {
  437. if((protocolCount > 4) && (protocolCount != tmpCount))
  438. {
  439. protocolBuff[0] = 0xFF;
  440. protocolBuff[1] = 0xFF;
  441. protocolCount = 2;
  442. }
  443. }
  444. }
  445. else if((0xFF == lastData) && (0x55 == tmpData))
  446. {
  447. }
  448. else
  449. {
  450. if(1 == protocolFlag)
  451. {
  452. protocolBuff[protocolCount] = tmpData;
  453. protocolCount++;

  454. if(protocolCount > 4)
  455. {
  456. head = (protocolHead_t *)protocolBuff;
  457. tmpCount = exchangeBytes(head->len)+4;
  458. if(protocolCount == tmpCount)
  459. {
  460. break;
  461. }
  462. }
  463. }
  464. }

  465. lastData = tmpData;
  466. debugCount++;
  467. }
  468. }

  469. if((protocolCount > 4) && (protocolCount == tmpCount))
  470. {
  471. sum = gizProtocolSum(protocolBuff, protocolCount);

  472. if(protocolBuff[protocolCount-1] == sum)
  473. {
  474. memcpy(gizdata, protocolBuff, tmpCount);
  475. *len = tmpCount;
  476. protocolFlag = 0;

  477. protocolCount = 0;
  478. debugCount = 0;
  479. lastData = 0;

  480. return 0;
  481. }
  482. else
  483. {
  484. return -2;
  485. }
  486. }

  487. return 1;
  488. }



  489. /**
  490. * @brief Protocol data resend

  491. * The protocol data resend when check timeout and meet the resend limiting

  492. * @param none
  493. *
  494. * @return none
  495. */
  496. static void gizProtocolResendData(void)
  497. {
  498. int32_t ret = 0;

  499. if(0 == gizwitsProtocol.waitAck.flag)
  500. {
  501. return;
  502. }

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

  504. ret = uartWrite(gizwitsProtocol.waitAck.buf, gizwitsProtocol.waitAck.dataLen);
  505. if(ret != gizwitsProtocol.waitAck.dataLen)
  506. {
  507. GIZWITS_LOG("ERR: resend data error ");
  508. }

  509. gizwitsProtocol.waitAck.sendTime = gizGetTimerCount();
  510. }

  511. /**
  512. * @brief Clear the ACK protocol message
  513. *
  514. * @param [in] head : Protocol header address
  515. *
  516. * @return 0, success; other, failure
  517. */
  518. static int8_t gizProtocolWaitAckCheck(protocolHead_t *head)
  519. {
  520. protocolHead_t *waitAckHead = (protocolHead_t *)gizwitsProtocol.waitAck.buf;

  521. if(NULL == head)
  522. {
  523. GIZWITS_LOG("ERR: data is empty ");
  524. return -1;
  525. }

  526. if(waitAckHead->cmd+1 == head->cmd)
  527. {
  528. memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));
  529. }

  530. return 0;
  531. }

  532. /**
  533. * @brief Send general protocol message data
  534. *
  535. * @param [in] head : Protocol header address
  536. *
  537. * @return : Return effective data length;-1,return failure
  538. */
  539. static int32_t gizProtocolCommonAck(protocolHead_t *head)
  540. {
  541. int32_t ret = 0;
  542. protocolCommon_t ack;

  543. if(NULL == head)
  544. {
  545. GIZWITS_LOG("ERR: gizProtocolCommonAck data is empty ");
  546. return -1;
  547. }
  548. memcpy((uint8_t *)&ack, (uint8_t *)head, sizeof(protocolHead_t));
  549. ack.head.cmd = ack.head.cmd+1;
  550. ack.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  551. ack.sum = gizProtocolSum((uint8_t *)&ack, sizeof(protocolCommon_t));

  552. ret = uartWrite((uint8_t *)&ack, sizeof(protocolCommon_t));
  553. if(ret < 0)
  554. {
  555. GIZWITS_LOG("ERR: uart write error %d ", ret);
  556. }

  557. return ret;
  558. }

  559. /**
  560. * @brief ACK processing function

  561. * Time-out 200ms no ACK resend,resend two times at most

  562. * @param none
  563. *
  564. * @return none
  565. */
  566. static void gizProtocolAckHandle(void)
  567. {
  568. if(1 == gizwitsProtocol.waitAck.flag)
  569. {
  570. if(SEND_MAX_NUM > gizwitsProtocol.waitAck.num)
  571. {
  572. // Time-out no ACK resend
  573. if(SEND_MAX_TIME < (gizGetTimerCount() - gizwitsProtocol.waitAck.sendTime))
  574. {
  575. GIZWITS_LOG("Warning:gizProtocolResendData %d %d %d ", gizGetTimerCount(), gizwitsProtocol.waitAck.sendTime, gizwitsProtocol.waitAck.num);
  576. gizProtocolResendData();
  577. gizwitsProtocol.waitAck.num++;
  578. }
  579. }
  580. else
  581. {
  582. memset((uint8_t *)&gizwitsProtocol.waitAck, 0, sizeof(protocolWaitAck_t));
  583. }
  584. }
  585. }

  586. /**
  587. * @brief Protocol 4.1 WiFi module requests device information
  588. *
  589. * @param[in] head : Protocol header address
  590. *
  591. * @return Return effective data length;-1,return failure
  592. */
  593. static int32_t gizProtocolGetDeviceInfo(protocolHead_t * head)
  594. {
  595. int32_t ret = 0;
  596. protocolDeviceInfo_t deviceInfo;

  597. if(NULL == head)
  598. {
  599. GIZWITS_LOG("gizProtocolGetDeviceInfo Error , Illegal Param ");
  600. return -1;
  601. }

  602. gizProtocolHeadInit((protocolHead_t *)&deviceInfo);
  603. deviceInfo.head.cmd = ACK_GET_DEVICE_INFO;
  604. deviceInfo.head.sn = head->sn;
  605. memcpy((uint8_t *)deviceInfo.protocolVer, protocol_VERSION, 8);
  606. memcpy((uint8_t *)deviceInfo.p0Ver, P0_VERSION, 8);
  607. memcpy((uint8_t *)deviceInfo.softVer, SOFTWARE_VERSION, 8);
  608. memcpy((uint8_t *)deviceInfo.hardVer, HARDWARE_VERSION, 8);
  609. memcpy((uint8_t *)deviceInfo.productKey, PRODUCT_KEY, strlen(PRODUCT_KEY));
  610. memcpy((uint8_t *)deviceInfo.productSecret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
  611. memset((uint8_t *)deviceInfo.devAttr, 0, 8);
  612. deviceInfo.devAttr[7] |= DEV_IS_GATEWAY<<0;
  613. deviceInfo.devAttr[7] |= (0x01<<1);
  614. deviceInfo.ninableTime = exchangeBytes(NINABLETIME);
  615. deviceInfo.head.len = exchangeBytes(sizeof(protocolDeviceInfo_t)-4);
  616. deviceInfo.sum = gizProtocolSum((uint8_t *)&deviceInfo, sizeof(protocolDeviceInfo_t));

  617. ret = uartWrite((uint8_t *)&deviceInfo, sizeof(protocolDeviceInfo_t));
  618. if(ret < 0)
  619. {
  620. GIZWITS_LOG("ERR: uart write error %d ", ret);
  621. }

  622. return ret;
  623. }

  624. /**
  625. * @brief Protocol 4.7 Handling of illegal message notification

  626. * @param[in] head : Protocol header address
  627. * @param[in] errno : Illegal message notification type
  628. * @return 0, success; other, failure
  629. */
  630. static int32_t gizProtocolErrorCmd(protocolHead_t *head,errorPacketsType_t errno)
  631. {
  632. int32_t ret = 0;
  633. protocolErrorType_t errorType;

  634. if(NULL == head)
  635. {
  636. GIZWITS_LOG("gizProtocolErrorCmd Error , Illegal Param ");
  637. return -1;
  638. }
  639. gizProtocolHeadInit((protocolHead_t *)&errorType);
  640. errorType.head.cmd = ACK_ERROR_PACKAGE;
  641. errorType.head.sn = head->sn;

  642. errorType.head.len = exchangeBytes(sizeof(protocolErrorType_t)-4);
  643. errorType.error = errno;
  644. errorType.sum = gizProtocolSum((uint8_t *)&errorType, sizeof(protocolErrorType_t));

  645. ret = uartWrite((uint8_t *)&errorType, sizeof(protocolErrorType_t));
  646. if(ret < 0)
  647. {
  648. GIZWITS_LOG("ERR: uart write error %d ", ret);
  649. }

  650. return ret;
  651. }

  652. /**
  653. * @brief Protocol 4.13 Get and process network time
  654. *
  655. * @param [in] head : Protocol header address
  656. *
  657. * @return 0, success; other, failure
  658. */
  659. static int8_t gizProtocolNTP(protocolHead_t *head)
  660. {
  661. protocolUTT_t *UTTInfo = (protocolUTT_t *)head;

  662. if(NULL == head)
  663. {
  664. GIZWITS_LOG("ERR: NTP is empty ");
  665. return -1;
  666. }

  667. memcpy((uint8_t *)&gizwitsProtocol.TimeNTP,(uint8_t *)UTTInfo->time, (7 + 4));
  668. gizwitsProtocol.TimeNTP.year = exchangeBytes(gizwitsProtocol.TimeNTP.year);
  669. gizwitsProtocol.TimeNTP.ntp =exchangeWord(gizwitsProtocol.TimeNTP.ntp);

  670. gizwitsProtocol.NTPEvent.event[gizwitsProtocol.NTPEvent.num] = WIFI_NTP;
  671. gizwitsProtocol.NTPEvent.num++;

  672. gizwitsProtocol.issuedFlag = GET_NTP_TYPE;


  673. return 0;
  674. }

  675. /**
  676. * @brief Protocol 4.4 Device MCU restarts function

  677. * @param none
  678. * @return none
  679. */
  680. static void gizProtocolReboot(void)
  681. {
  682. uint32_t timeDelay = gizGetTimerCount();

  683. /*Wait 600ms*/
  684. while((gizGetTimerCount() - timeDelay) <= 600);
  685. mcuRestart();
  686. }

  687. /**
  688. * @brief Protocol 4.5 :The WiFi module informs the device MCU of working status about the WiFi module

  689. * @param[in] status WiFi module working status
  690. * @return none
  691. */
  692. static int8_t gizProtocolModuleStatus(protocolWifiStatus_t *status)
  693. {
  694. static wifiStatus_t lastStatus;

  695. if(NULL == status)
  696. {
  697. GIZWITS_LOG("gizProtocolModuleStatus Error , Illegal Param ");
  698. return -1;
  699. }

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

  701. //OnBoarding mode status
  702. if(lastStatus.types.onboarding != status->ststus.types.onboarding)
  703. {
  704. if(1 == status->ststus.types.onboarding)
  705. {
  706. if(1 == status->ststus.types.softap)
  707. {
  708. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_SOFTAP;
  709. gizwitsProtocol.wifiStatusEvent.num++;
  710. GIZWITS_LOG("OnBoarding: SoftAP or Web mode ");
  711. }

  712. if(1 == status->ststus.types.station)
  713. {
  714. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_AIRLINK;
  715. gizwitsProtocol.wifiStatusEvent.num++;
  716. GIZWITS_LOG("OnBoarding: AirLink mode ");
  717. }
  718. }
  719. else
  720. {
  721. if(1 == status->ststus.types.softap)
  722. {
  723. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_SOFTAP;
  724. gizwitsProtocol.wifiStatusEvent.num++;
  725. GIZWITS_LOG("OnBoarding: SoftAP or Web mode ");
  726. }

  727. if(1 == status->ststus.types.station)
  728. {
  729. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_STATION;
  730. gizwitsProtocol.wifiStatusEvent.num++;
  731. GIZWITS_LOG("OnBoarding: Station mode ");
  732. }
  733. }
  734. }

  735. //binding mode status
  736. if(lastStatus.types.binding != status->ststus.types.binding)
  737. {
  738. lastStatus.types.binding = status->ststus.types.binding;
  739. if(1 == status->ststus.types.binding)
  740. {
  741. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_OPEN_BINDING;
  742. gizwitsProtocol.wifiStatusEvent.num++;
  743. GIZWITS_LOG("WiFi status: in binding mode ");
  744. }
  745. else
  746. {
  747. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CLOSE_BINDING;
  748. gizwitsProtocol.wifiStatusEvent.num++;
  749. GIZWITS_LOG("WiFi status: out binding mode ");
  750. }
  751. }

  752. //router status
  753. if(lastStatus.types.con_route != status->ststus.types.con_route)
  754. {
  755. lastStatus.types.con_route = status->ststus.types.con_route;
  756. if(1 == status->ststus.types.con_route)
  757. {
  758. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_ROUTER;
  759. gizwitsProtocol.wifiStatusEvent.num++;
  760. GIZWITS_LOG("WiFi status: connected router ");
  761. }
  762. else
  763. {
  764. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_ROUTER;
  765. gizwitsProtocol.wifiStatusEvent.num++;
  766. GIZWITS_LOG("WiFi status: disconnected router ");
  767. }
  768. }

  769. //M2M server status
  770. if(lastStatus.types.con_m2m != status->ststus.types.con_m2m)
  771. {
  772. lastStatus.types.con_m2m = status->ststus.types.con_m2m;
  773. if(1 == status->ststus.types.con_m2m)
  774. {
  775. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_M2M;
  776. gizwitsProtocol.wifiStatusEvent.num++;
  777. GIZWITS_LOG("WiFi status: connected m2m ");
  778. }
  779. else
  780. {
  781. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_M2M;
  782. gizwitsProtocol.wifiStatusEvent.num++;
  783. GIZWITS_LOG("WiFi status: disconnected m2m ");
  784. }
  785. }

  786. //APP status
  787. if(lastStatus.types.app != status->ststus.types.app)
  788. {
  789. lastStatus.types.app = status->ststus.types.app;
  790. if(1 == status->ststus.types.app)
  791. {
  792. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CON_APP;
  793. gizwitsProtocol.wifiStatusEvent.num++;
  794. GIZWITS_LOG("WiFi status: app connect ");
  795. }
  796. else
  797. {
  798. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_DISCON_APP;
  799. gizwitsProtocol.wifiStatusEvent.num++;
  800. GIZWITS_LOG("WiFi status: no app connect ");
  801. }
  802. }

  803. //test mode status
  804. if(lastStatus.types.test != status->ststus.types.test)
  805. {
  806. lastStatus.types.test = status->ststus.types.test;
  807. if(1 == status->ststus.types.test)
  808. {
  809. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_OPEN_TESTMODE;
  810. gizwitsProtocol.wifiStatusEvent.num++;
  811. GIZWITS_LOG("WiFi status: in test mode ");
  812. }
  813. else
  814. {
  815. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_CLOSE_TESTMODE;
  816. gizwitsProtocol.wifiStatusEvent.num++;
  817. GIZWITS_LOG("WiFi status: out test mode ");
  818. }
  819. }

  820. gizwitsProtocol.wifiStatusEvent.event[gizwitsProtocol.wifiStatusEvent.num] = WIFI_RSSI;
  821. gizwitsProtocol.wifiStatusEvent.num++;
  822. gizwitsProtocol.wifiStatusData.rssi = status->ststus.types.rssi;
  823. GIZWITS_LOG("RSSI is %d ", gizwitsProtocol.wifiStatusData.rssi);

  824. gizwitsProtocol.issuedFlag = WIFI_STATUS_TYPE;

  825. return 0;
  826. }


  827. /**@name Gizwits User API interface
  828. * @{
  829. */

  830. /**
  831. * @brief gizwits Protocol initialization interface

  832. * Protocol-related timer, serial port initialization

  833. * Datapoint initialization

  834. * @param none
  835. * @return none
  836. */
  837. void gizwitsInit(void)
  838. {
  839. pRb.rbCapacity = RB_MAX_LEN;
  840. pRb.rbBuff = rbBuf;
  841. if(0 == rbCreate(&pRb))
  842. {
  843. GIZWITS_LOG("rbCreate Success ");
  844. }
  845. else
  846. {
  847. GIZWITS_LOG("rbCreate Faild ");
  848. }

  849. memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));
  850. }

  851. /**
  852. * @brief WiFi configure interface

  853. * Set the WiFi module into the corresponding configuration mode or reset the module

  854. * @param[in] mode :0x0, reset the module ;0x01, SoftAp mode ;0x02, AirLink mode ;0x03, Production test mode; 0x04:allow users to bind devices

  855. * @return Error command code
  856. */
  857. int32_t gizwitsSetMode(uint8_t mode)
  858. {
  859. int32_t ret = 0;
  860. protocolCfgMode_t cfgMode;
  861. protocolCommon_t setDefault;

  862. switch(mode)
  863. {
  864. case WIFI_RESET_MODE:
  865. gizProtocolHeadInit((protocolHead_t *)&setDefault);
  866. setDefault.head.cmd = CMD_SET_DEFAULT;
  867. setDefault.head.sn = gizwitsProtocol.sn++;
  868. setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  869. setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  870. ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  871. if(ret < 0)
  872. {
  873. GIZWITS_LOG("ERR: uart write error %d ", ret);
  874. }

  875. gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  876. break;
  877. case WIFI_SOFTAP_MODE:
  878. gizProtocolHeadInit((protocolHead_t *)&cfgMode);
  879. cfgMode.head.cmd = CMD_WIFI_CONFIG;
  880. cfgMode.head.sn = gizwitsProtocol.sn++;
  881. cfgMode.cfgMode = mode;
  882. cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);
  883. cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  884. ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  885. if(ret < 0)
  886. {
  887. GIZWITS_LOG("ERR: uart write error %d ", ret);
  888. }
  889. gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  890. break;
  891. case WIFI_AIRLINK_MODE:
  892. gizProtocolHeadInit((protocolHead_t *)&cfgMode);
  893. cfgMode.head.cmd = CMD_WIFI_CONFIG;
  894. cfgMode.head.sn = gizwitsProtocol.sn++;
  895. cfgMode.cfgMode = mode;
  896. cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);
  897. cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  898. ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  899. if(ret < 0)
  900. {
  901. GIZWITS_LOG("ERR: uart write error %d ", ret);
  902. }
  903. gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  904. break;
  905. case WIFI_PRODUCTION_TEST:
  906. gizProtocolHeadInit((protocolHead_t *)&setDefault);
  907. setDefault.head.cmd = CMD_PRODUCTION_TEST;
  908. setDefault.head.sn = gizwitsProtocol.sn++;
  909. setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  910. setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  911. ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  912. if(ret < 0)
  913. {
  914. GIZWITS_LOG("ERR: uart write error %d ", ret);
  915. }

  916. gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  917. break;
  918. case WIFI_NINABLE_MODE:
  919. gizProtocolHeadInit((protocolHead_t *)&setDefault);
  920. setDefault.head.cmd = CMD_NINABLE_MODE;
  921. setDefault.head.sn = gizwitsProtocol.sn++;
  922. setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  923. setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  924. ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  925. if(ret < 0)
  926. {
  927. GIZWITS_LOG("ERR: uart write error %d ", ret);
  928. }

  929. gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  930. break;
  931. case WIFI_REBOOT_MODE:
  932. gizProtocolHeadInit((protocolHead_t *)&setDefault);
  933. setDefault.head.cmd = CMD_REBOOT_MODULE;
  934. setDefault.head.sn = gizwitsProtocol.sn++;
  935. setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  936. setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  937. ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  938. if(ret < 0)
  939. {
  940. GIZWITS_LOG("ERR: uart write error %d ", ret);
  941. }

  942. gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  943. break;
  944. default:
  945. GIZWITS_LOG("ERR: CfgMode error! ");
  946. break;
  947. }

  948. return ret;
  949. }

  950. /**
  951. * @brief Get the the network time

  952. * Protocol 4.13:"Device MCU send" of "the MCU requests access to the network time"

  953. * @param[in] none
  954. * @return none
  955. */
  956. void gizwitsGetNTP(void)
  957. {
  958. int32_t ret = 0;
  959. protocolCommon_t getNTP;

  960. gizProtocolHeadInit((protocolHead_t *)&getNTP);
  961. getNTP.head.cmd = CMD_GET_NTP;
  962. getNTP.head.sn = gizwitsProtocol.sn++;
  963. getNTP.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  964. getNTP.sum = gizProtocolSum((uint8_t *)&getNTP, sizeof(protocolCommon_t));
  965. ret = uartWrite((uint8_t *)&getNTP, sizeof(protocolCommon_t));
  966. if(ret < 0)
  967. {
  968. GIZWITS_LOG("ERR[NTP]: uart write error %d ", ret);
  969. }

  970. gizProtocolWaitAck((uint8_t *)&getNTP, sizeof(protocolCommon_t));
  971. }


  972. /**
  973. * @brief Get Module Info

  974. *

  975. * @param[in] none
  976. * @return none
  977. */
  978. void gizwitsGetModuleInfo(void)
  979. {
  980. int32_t ret = 0;
  981. protocolGetModuleInfo_t getModuleInfo;

  982. gizProtocolHeadInit((protocolHead_t *)&getModuleInfo);
  983. getModuleInfo.head.cmd = CMD_ASK_MODULE_INFO;
  984. getModuleInfo.head.sn = gizwitsProtocol.sn++;
  985. getModuleInfo.type = 0x0;
  986. getModuleInfo.head.len = exchangeBytes(sizeof(protocolGetModuleInfo_t)-4);
  987. getModuleInfo.sum = gizProtocolSum((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));
  988. ret = uartWrite((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));
  989. if(ret < 0)
  990. {
  991. GIZWITS_LOG("ERR[NTP]: uart write error %d ", ret);
  992. }

  993. gizProtocolWaitAck((uint8_t *)&getModuleInfo, sizeof(protocolGetModuleInfo_t));
  994. }


  995. /**
  996. * @brief Module Info Analyse
  997. *
  998. * @param [in] head :
  999. *
  1000. * @return 0, Success, , other,Faild
  1001. */
  1002. static int8_t gizProtocolModuleInfoHandle(protocolHead_t *head)
  1003. {
  1004. protocolModuleInfo_t *moduleInfo = (protocolModuleInfo_t *)head;

  1005. if(NULL == head)
  1006. {
  1007. GIZWITS_LOG("NTP is empty ");
  1008. return -1;
  1009. }

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

  1011. gizwitsProtocol.moduleInfoEvent.event[gizwitsProtocol.moduleInfoEvent.num] = MODULE_INFO;
  1012. gizwitsProtocol.moduleInfoEvent.num++;

  1013. gizwitsProtocol.issuedFlag = GET_MODULEINFO_TYPE;


  1014. return 0;
  1015. }

  1016. /**
  1017. * @brief Protocol handling function

  1018. *

  1019. * @param [in] currentData :The protocol data pointer
  1020. * @return none
  1021. */
  1022. int32_t gizwitsHandle(dataPoint_t *currentData)
  1023. {
  1024. int8_t ret = 0;
  1025. #ifdef PROTOCOL_DEBUG
  1026. uint16_t i = 0;
  1027. #endif
  1028. uint8_t ackData[RB_MAX_LEN];
  1029. uint16_t protocolLen = 0;
  1030. uint32_t ackLen = 0;
  1031. protocolHead_t *recvHead = NULL;
  1032. char *didPtr = NULL;
  1033. uint16_t offset = 0;


  1034. if(NULL == currentData)
  1035. {
  1036. GIZWITS_LOG("GizwitsHandle Error , Illegal Param ");
  1037. return -1;
  1038. }

  1039. /*resend strategy*/
  1040. gizProtocolAckHandle();
  1041. ret = gizProtocolGetOnePacket(&pRb, gizwitsProtocol.protocolBuf, &protocolLen);

  1042. if(0 == ret)
  1043. {
  1044. GIZWITS_LOG("Get One Packet! ");

  1045. #ifdef PROTOCOL_DEBUG
  1046. GIZWITS_LOG("WiFi2MCU[%4d:%4d]: ", gizGetTimerCount(), protocolLen);
  1047. for(i=0; i
  1048. {
  1049. GIZWITS_LOG("%02x ", gizwitsProtocol.protocolBuf[i]);
  1050. }
  1051. GIZWITS_LOG(" ");
  1052. #endif

  1053. recvHead = (protocolHead_t *)gizwitsProtocol.protocolBuf;
  1054. switch (recvHead->cmd)
  1055. {
  1056. case CMD_GET_DEVICE_INTO:
  1057. gizProtocolGetDeviceInfo(recvHead);
  1058. break;
  1059. case CMD_ISSUED_P0:
  1060. GIZWITS_LOG("flag %x %x ", recvHead->flags[0], recvHead->flags[1]);
  1061. //offset = 1;

  1062. if(0 == gizProtocolIssuedProcess(didPtr, gizwitsProtocol.protocolBuf+sizeof(protocolHead_t)+offset, protocolLen-(sizeof(protocolHead_t)+offset+1), ackData, &ackLen))
  1063. {
  1064. gizProtocolIssuedDataAck(recvHead, ackData, ackLen,recvHead->flags[1]);
  1065. GIZWITS_LOG("AckData : ");
  1066. }
  1067. break;
  1068. case CMD_HEARTBEAT:
  1069. gizProtocolCommonAck(recvHead);
  1070. break;
  1071. case CMD_WIFISTATUS:
  1072. gizProtocolCommonAck(recvHead);
  1073. gizProtocolModuleStatus((protocolWifiStatus_t *)recvHead);
  1074. break;
  1075. case ACK_REPORT_P0:
  1076. case ACK_WIFI_CONFIG:
  1077. case ACK_SET_DEFAULT:
  1078. case ACK_NINABLE_MODE:
  1079. case ACK_REBOOT_MODULE:
  1080. gizProtocolWaitAckCheck(recvHead);
  1081. break;
  1082. case CMD_MCU_REBOOT:
  1083. gizProtocolCommonAck(recvHead);
  1084. GIZWITS_LOG("report:MCU reboot! ");

  1085. gizProtocolReboot();
  1086. break;
  1087. case CMD_ERROR_PACKAGE:
  1088. break;
  1089. case ACK_PRODUCTION_TEST:
  1090. gizProtocolWaitAckCheck(recvHead);
  1091. GIZWITS_LOG("Ack PRODUCTION_MODE success ");
  1092. break;
  1093. case ACK_GET_NTP:
  1094. gizProtocolWaitAckCheck(recvHead);
  1095. gizProtocolNTP(recvHead);
  1096. GIZWITS_LOG("Ack GET_UTT success ");
  1097. break;
  1098. case ACK_ASK_MODULE_INFO:
  1099. gizProtocolWaitAckCheck(recvHead);
  1100. gizProtocolModuleInfoHandle(recvHead);
  1101. GIZWITS_LOG("Ack GET_Module success ");
  1102. break;

  1103. default:
  1104. gizProtocolErrorCmd(recvHead,ERROR_CMD);
  1105. GIZWITS_LOG("ERR: cmd code error! ");
  1106. break;
  1107. }
  1108. }
  1109. else if(-2 == ret)
  1110. {
  1111. //Check failed, report exception
  1112. recvHead = (protocolHead_t *)gizwitsProtocol.protocolBuf;
  1113. gizProtocolErrorCmd(recvHead,ERROR_ACK_SUM);
  1114. GIZWITS_LOG("ERR: check sum error! ");
  1115. return -2;
  1116. }

  1117. switch(gizwitsProtocol.issuedFlag)
  1118. {
  1119. case ACTION_CONTROL_TYPE:
  1120. gizwitsProtocol.issuedFlag = STATELESS_TYPE;
  1121. gizwitsEventProcess(&gizwitsProtocol.issuedProcessEvent, (uint8_t *)&gizwitsProtocol.gizCurrentDataPoint, sizeof(dataPoint_t));
  1122. memset((uint8_t *)&gizwitsProtocol.issuedProcessEvent,0x0,sizeof(gizwitsProtocol.issuedProcessEvent));
  1123. break;
  1124. case WIFI_STATUS_TYPE:
  1125. gizwitsProtocol.issuedFlag = STATELESS_TYPE;
  1126. gizwitsEventProcess(&gizwitsProtocol.wifiStatusEvent, (uint8_t *)&gizwitsProtocol.wifiStatusData, sizeof(moduleStatusInfo_t));
  1127. memset((uint8_t *)&gizwitsProtocol.wifiStatusEvent,0x0,sizeof(gizwitsProtocol.wifiStatusEvent));
  1128. break;
  1129. case ACTION_W2D_TRANSPARENT_TYPE:
  1130. gizwitsProtocol.issuedFlag = STATELESS_TYPE;
  1131. gizwitsEventProcess(&gizwitsProtocol.issuedProcessEvent, (uint8_t *)gizwitsProtocol.transparentBuff, gizwitsProtocol.transparentLen);
  1132. break;
  1133. case GET_NTP_TYPE:
  1134. gizwitsProtocol.issuedFlag = STATELESS_TYPE;
  1135. gizwitsEventProcess(&gizwitsProtocol.NTPEvent, (uint8_t *)&gizwitsProtocol.TimeNTP, sizeof(protocolTime_t));
  1136. memset((uint8_t *)&gizwitsProtocol.NTPEvent,0x0,sizeof(gizwitsProtocol.NTPEvent));
  1137. break;
  1138. case GET_MODULEINFO_TYPE:
  1139. gizwitsProtocol.issuedFlag = STATELESS_TYPE;
  1140. gizwitsEventProcess(&gizwitsProtocol.moduleInfoEvent, (uint8_t *)&gizwitsProtocol.wifiModuleNews, sizeof(moduleInfo_t));
  1141. memset((uint8_t *)&gizwitsProtocol.moduleInfoEvent,0x0,sizeof(moduleInfo_t));
  1142. break;
  1143. default:
  1144. break;
  1145. }

  1146. gizDevReportPolicy(currentData);

  1147. return 0;
  1148. }

  1149. /**
  1150. * @brief gizwits report transparent data interface

  1151. * The user can call the interface to complete the reporting of private protocol data

  1152. * @param [in] data :Private protocol data
  1153. * @param [in] len :Private protocol data length
  1154. * @return 0,success ;other,failure
  1155. */
  1156. int32_t gizwitsPassthroughData(uint8_t * gizdata, uint32_t len)
  1157. {
  1158. int32_t ret = 0;
  1159. uint8_t tx_buf[MAX_PACKAGE_LEN];
  1160. uint8_t *pTxBuf = tx_buf;
  1161. uint16_t data_len = 6+len;
  1162. if(NULL == gizdata)
  1163. {
  1164. GIZWITS_LOG("[ERR] gizwitsPassthroughData Error ");
  1165. return (-1);
  1166. }

  1167. *pTxBuf ++= 0xFF;
  1168. *pTxBuf ++= 0xFF;
  1169. *pTxBuf ++= (uint8_t)(data_len>>8);//len
  1170. *pTxBuf ++= (uint8_t)(data_len);
  1171. *pTxBuf ++= CMD_REPORT_P0;//0x1b cmd
  1172. *pTxBuf ++= gizwitsProtocol.sn++;//sn
  1173. *pTxBuf ++= 0x00;//flag
  1174. *pTxBuf ++= 0x00;//flag
  1175. *pTxBuf ++= ACTION_D2W_TRANSPARENT_DATA;//P0_Cmd

  1176. memcpy(&tx_buf[9],gizdata,len);
  1177. tx_buf[data_len + 4 - 1 ] = gizProtocolSum( tx_buf , (data_len+4));

  1178. ret = uartWrite(tx_buf, data_len+4);
  1179. if(ret < 0)
  1180. {
  1181. GIZWITS_LOG("ERR: uart write error %d ", ret);
  1182. }

  1183. gizProtocolWaitAck(tx_buf, data_len+4);

  1184. return 0;
  1185. }


  1186. void gziwits_Task(dataPoint_t * currentDataPoint)
  1187. {
  1188. static uint32_t Timer=0;
  1189. if(SoftTimer(Timer,5000))
  1190. {
  1191. gizwitsHandle(currentDataPoint);
  1192. Timer=GetSoftTimer();
  1193. }
  1194. }


  1195. /**@} */


复制代码




修改gizwits_protocol.h



  1. #ifndef _GIZWITS_PROTOCOL_H
  2. #define _GIZWITS_PROTOCOL_H

  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif

  6. #include
  7. #include
  8. #include
  9. #include
  10. #include
  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. */
  19. #define PRODUCT_KEY "9c8a5a8e38344fb4af14b6db0f5b1df7"
  20. /**@} */
  21. /**@name Product Secret
  22. * @{
  23. */
  24. #define PRODUCT_SECRET "45c86d8c6a2a4b1dac7d68df54f6e4f0"

  25. /**@name Device status data reporting interval
  26. * @{
  27. */
  28. #define REPORT_TIME_MAX 6000 //6S
  29. /**@} */

  30. #define CELLNUMMAX 7


  31. /**@name Whether the device is in the control class, 0 means no, 1 means yes
  32. * @{
  33. */
  34. #define DEV_IS_GATEWAY 0
  35. /**@} */

  36. /**@name Binding time
  37. * @{
  38. */
  39. #define NINABLETIME 0
  40. /**@} */



  41. #define MAX_PACKAGE_LEN (sizeof(devStatus_t)+sizeof(attrFlags_t)+20) ///< Data buffer maximum length
  42. #define RB_MAX_LEN (MAX_PACKAGE_LEN*2) ///< Maximum length of ring buffer

  43. /**@name Data point related definition
  44. * @{
  45. */
  46. #define Relay_1_BYTEOFFSET 0
  47. #define Relay_1_BITOFFSET 0
  48. #define Relay_1_LEN 1

  49. #define Temp_RATIO 1
  50. #define Temp_ADDITION 0
  51. #define Temp_MIN 0
  52. #define Temp_MAX 100
  53. #define Humi_RATIO 1
  54. #define Humi_ADDITION 0
  55. #define Humi_MIN 0
  56. #define Humi_MAX 100
  57. #define Light_Intensity_RATIO 1
  58. #define Light_Intensity_ADDITION 0
  59. #define Light_Intensity_MIN 0
  60. #define Light_Intensity_MAX 100
  61. /**@} */

  62. /** Writable data points Boolean and enumerated variables occupy byte size */
  63. #define COUNT_W_BIT 1







  64. /** Event enumeration */
  65. typedef enum
  66. {
  67. WIFI_SOFTAP = 0x00, ///< WiFi SOFTAP configuration event
  68. WIFI_AIRLINK, ///< WiFi module AIRLINK configuration event
  69. WIFI_STATION, ///< WiFi module STATION configuration event
  70. WIFI_OPEN_BINDING, ///< The WiFi module opens the binding event
  71. WIFI_CLOSE_BINDING, ///< The WiFi module closes the binding event
  72. WIFI_CON_ROUTER, ///< The WiFi module is connected to a routing event
  73. WIFI_DISCON_ROUTER, ///< The WiFi module has been disconnected from the routing event
  74. WIFI_CON_M2M, ///< The WiFi module has a server M2M event
  75. WIFI_DISCON_M2M, ///< The WiFi module has been disconnected from the server M2M event
  76. WIFI_OPEN_TESTMODE, ///< The WiFi module turns on the test mode event
  77. WIFI_CLOSE_TESTMODE, ///< The WiFi module turns off the test mode event
  78. WIFI_CON_APP, ///< The WiFi module connects to the APP event
  79. WIFI_DISCON_APP, ///< The WiFi module disconnects the APP event
  80. WIFI_RSSI, ///< WiFi module RSSI event
  81. WIFI_NTP, ///< Network time event
  82. MODULE_INFO, ///< Module information event
  83. TRANSPARENT_DATA, ///< Transparency events
  84. EVENT_Relay_1,
  85. EVENT_TYPE_MAX ///< Enumerate the number of members to calculate (user accidentally deleted)
  86. } EVENT_TYPE_T;

  87. /** P0 Command code */
  88. typedef enum
  89. {
  90. ACTION_CONTROL_DEVICE = 0x01, ///< Protocol 4.10 WiFi Module Control Device WiFi Module Send
  91. ACTION_READ_DEV_STATUS = 0x02, ///< Protocol 4.8 WiFi Module Reads the current status of the device WiFi module sent
  92. ACTION_READ_DEV_STATUS_ACK = 0x03, ///< Protocol 4.8 WiFi Module Read Device Current Status Device MCU Reply
  93. 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
  94. ACTION_W2D_TRANSPARENT_DATA = 0x05, ///< WiFi to device MCU transparent
  95. ACTION_D2W_TRANSPARENT_DATA = 0x06, ///< Device MCU to WiFi
  96. } actionType_t;

  97. /** Protocol network time structure */
  98. typedef struct
  99. {
  100. uint16_t year;
  101. uint8_t month;
  102. uint8_t day;
  103. uint8_t hour;
  104. uint8_t minute;
  105. uint8_t second;
  106. uint32_t ntp;
  107. }protocolTime_t;


  108. /** WiFi Module configuration parameters*/
  109. typedef enum
  110. {
  111. WIFI_RESET_MODE = 0x00, ///< WIFI module reset
  112. WIFI_SOFTAP_MODE, ///< WIFI module softAP modeF
  113. WIFI_AIRLINK_MODE, ///< WIFI module AirLink mode
  114. WIFI_PRODUCTION_TEST, ///< MCU request WiFi module into production test mode
  115. WIFI_NINABLE_MODE, ///< MCU request module to enter binding mode
  116. WIFI_REBOOT_MODE, ///< MCU request module reboot
  117. }WIFI_MODE_TYPE_T;

  118. /** The protocol event type*/
  119. typedef enum
  120. {
  121. STATELESS_TYPE = 0x00, ///< Stateless type
  122. ACTION_CONTROL_TYPE, ///< Protocol 4.10 :WiFi module control device event
  123. WIFI_STATUS_TYPE, ///< Protocol 4.5 :WiFi module inform the device MCU of the change event of the WiFi module status
  124. ACTION_W2D_TRANSPARENT_TYPE, ///< Protocol WiFi to device MCU transparent event
  125. GET_NTP_TYPE, ///< Protocol 4.13 :The MCU requests access to the network time event
  126. GET_MODULEINFO_TYPE, ///< Protocol 4.9 :The MCU get module information event
  127. PROTOCOL_EVENT_TYPE_MAX ///< Count enumerated member (User donot delete)
  128. } PROTOCOL_EVENT_TYPE_T;

  129. /** Protocol command code */
  130. typedef enum
  131. {
  132. CMD_GET_DEVICE_INTO = 0x01, ///< Protocol:3.1
  133. ACK_GET_DEVICE_INFO = 0x02, ///< Protocol:3.1

  134. CMD_ISSUED_P0 = 0x03, ///< Protocol:3.2 3.3
  135. ACK_ISSUED_P0 = 0x04, ///< Protocol:3.2 3.3

  136. CMD_REPORT_P0 = 0x05, ///< Protocol:3.4
  137. ACK_REPORT_P0 = 0x06, ///< Protocol:3.4

  138. CMD_HEARTBEAT = 0x07, ///< Protocol:3.5
  139. ACK_HEARTBEAT = 0x08, ///< Protocol:3.5

  140. CMD_WIFI_CONFIG = 0x09, ///< Protocol:3.6
  141. ACK_WIFI_CONFIG = 0x0A, ///< Protocol:3.6

  142. CMD_SET_DEFAULT = 0x0B, ///< Protocol:3.7
  143. ACK_SET_DEFAULT = 0x0C, ///< Protocol:3.7

  144. CMD_WIFISTATUS = 0x0D, ///< Protocol:3.8
  145. ACK_WIFISTATUS = 0x0E, ///< Protocol:3.8

  146. CMD_MCU_REBOOT = 0x0F, ///< Protocol:4.1
  147. ACK_MCU_REBOOT = 0x10, ///< Protocol:4.1

  148. CMD_ERROR_PACKAGE = 0x11, ///< Protocol:3.9
  149. ACK_ERROR_PACKAGE = 0x12, ///< Protocol:3.9

  150. CMD_PRODUCTION_TEST = 0x13, ///< Protocol:
  151. ACK_PRODUCTION_TEST = 0x14, ///< Protocol:

  152. CMD_NINABLE_MODE = 0x15, ///< Protocol:3.10
  153. ACK_NINABLE_MODE = 0x16, ///< Protocol:3.10

  154. CMD_GET_NTP = 0x17, ///< Protocol:4.3
  155. ACK_GET_NTP = 0x18, ///< Protocol:4.3


  156. CMD_ASK_BIGDATA = 0x19, ///< Protocol:4.4
  157. ACK_ASK_BIGDATA = 0x1A, ///< Protocol:4.4

  158. CMD_BIGDATA_READY = 0x1B, ///< Protocol:4.5
  159. ACK_BIGDATA_READY = 0x1C, ///< Protocol:4.5

  160. CMD_BIGDATA_SEND = 0x1D, ///< Protocol:4.6
  161. ACK_BIGDATA_SEND = 0x1E, ///< Protocol:4.6

  162. CMD_S_STOP_BIGDATA_SEND = 0x1F, ///< Protocol:4.7
  163. ACK_S_STOP_BIGDATA_SEND = 0x20, ///< Protocol:4.7

  164. CMD_D_STOP_BIGDATA_SEND = 0x27, ///< Protocol:4.8
  165. ACK_D_STOP_BIGDATA_SEND = 0x28, ///< Protocol:4.8

  166. CMD_ASK_MODULE_INFO = 0x21, ///< Protocol:4.9
  167. ACK_ASK_MODULE_INFO = 0x22, ///< Protocol:4.9

  168. CMD_ASK_AFFAIR_HANDLE = 0x23, ///< Protocol:4.10
  169. ACK_ASK_AFFAIR_HANDLE = 0x24, ///< Protocol:4.10

  170. CMD_AFFAIR_RESULT = 0x25, ///< Protocol:4.10
  171. ACK_AFFAIR_RESULT = 0x26, ///< Protocol:4.10

  172. CMD_REBOOT_MODULE = 0x29, ///< Protocol:3.11
  173. ACK_REBOOT_MODULE = 0x2A, ///< Protocol:3.11

  174. CMD_CONNECT_M2M = 0x2D, ///< Protocol:for Virtualization
  175. ACK_CONNECT_M2M = 0x2E, ///< Protocol:for Virtualization

  176. CMD_CONNECT_M2M_BACK = 0x2F, ///< Protocol:for Virtualization
  177. ACK_CONNECT_M2M_BACK = 0x30, ///< Protocol:for Virtualization

  178. CMD_UPLOAD_DATA = 0x31, ///< Protocol:for Virtualization
  179. ACK_UPLOAD_DATA = 0x32, ///< Protocol:for Virtualization

  180. CMD_UPLOAD_DATA_BACK = 0x33, ///< Protocol:for Virtualization
  181. ACK_UPLOAD_DATA_BACK = 0x34, ///< Protocol:for Virtualization

  182. CMD_DISCONNECT_M2M = 0x35, ///< Protocol:for Virtualization
  183. ACK_DISCONNECT_M2M = 0x36, ///< Protocol:for Virtualization

  184. CMD_DISCONNECT_M2M_BACK = 0x37, ///< Protocol:for Virtualization
  185. ACK_DISCONNECT_M2M_BACK = 0x38, ///< Protocol:for Virtualization

  186. CMD_RESET_SIMULATOR = 0x39, ///< Protocol:for Virtualization
  187. ACK_RESET_SIMULATOR = 0x3A, ///< Protocol:for Virtualization

  188. CMD_RESET_SIMULATOR_BACK = 0x3B, ///< Protocol:for Virtualization
  189. ACK_RESET_SIMULATOR_BACK = 0x3C, ///< Protocol:for Virtualization
  190. } PROTOCOL_CMDTYPE;

  191. /** Illegal message type*/
  192. typedef enum
  193. {
  194. ERROR_ACK_SUM = 0x01, ///< check error
  195. ERROR_CMD = 0x02, ///< Command code error
  196. ERROR_OTHER = 0x03, ///< other
  197. } errorPacketsType_t;

  198. typedef enum
  199. {
  200. EXE_SUCESS = 0x00,
  201. EXE_FAILE = 0x01,
  202. } execute_result;

  203. #pragma pack(1)

  204. /** User Area Device State Structure */
  205. typedef struct {
  206. bool valueRelay_1;
  207. uint32_t valueTemp;
  208. uint32_t valueHumi;
  209. uint32_t valueLight_Intensity;
  210. } dataPoint_t;


  211. /** Corresponding to the protocol "4.10 WiFi module control device" in the flag " attr_flags" */
  212. typedef struct {
  213. uint8_t flagRelay_1:1;
  214. } attrFlags_t;


  215. /** Corresponding protocol "4.10 WiFi module control device" in the data value "attr_vals" */

  216. typedef struct {
  217. uint8_t wBitBuf[COUNT_W_BIT];
  218. } attrVals_t;

  219. /** The flag "attr_flags (1B)" + data value "P0 protocol area" in the corresponding protocol "4.10 WiFi module control device"attr_vals(6B)" */
  220. typedef struct {
  221. attrFlags_t attrFlags;
  222. attrVals_t attrVals;
  223. }gizwitsIssued_t;

  224. /** Corresponding protocol "4.9 Device MCU to the WiFi module to actively report the current state" in the device status "dev_status(11B)" */

  225. typedef struct {
  226. uint8_t wBitBuf[COUNT_W_BIT];
  227. uint8_t valueTemp;
  228. uint8_t valueHumi;
  229. uint8_t valueLight_Intensity;
  230. } devStatus_t;



  231. /** Event queue structure */
  232. typedef struct {
  233. uint8_t num; ///< Number of queue member
  234. uint8_t event[EVENT_TYPE_MAX]; ///< Queue member event content
  235. }eventInfo_t;



  236. /** wifiSignal strength structure */
  237. typedef struct {
  238. uint8_t rssi; ///< WIFI signal strength
  239. }moduleStatusInfo_t;

  240. /** Protocol standard header structure */
  241. typedef struct
  242. {
  243. uint8_t head[2]; ///< The head is 0xFFFF
  244. uint16_t len; ///< From cmd to the end of the entire packet occupied by the number of bytes
  245. uint8_t cmd; ///< command
  246. uint8_t sn; ///<
  247. uint8_t flags[2]; ///< flag,default is 0
  248. } protocolHead_t;

  249. /** 4.1 WiFi module requests the device information protocol structure */
  250. typedef struct
  251. {
  252. protocolHead_t head; ///< Protocol standard header structure
  253. uint8_t protocolVer[8]; ///< Protocol version
  254. uint8_t p0Ver[8]; ///< p0 Protocol version
  255. uint8_t hardVer[8]; ///< Hardware version
  256. uint8_t softVer[8]; ///< Software version
  257. uint8_t productKey[32]; ///< Product key
  258. uint16_t ninableTime; ///< Binding time(second)
  259. uint8_t devAttr[8]; ///< Device attribute
  260. uint8_t productSecret[32]; ///< Product secret
  261. uint8_t sum; ///< checksum
  262. } protocolDeviceInfo_t;

  263. /** Protocol common data frame(4.2、4.4、4.6、4.9、4.10) protocol structure */
  264. typedef struct
  265. {
  266. protocolHead_t head; ///< Protocol standard header structure
  267. uint8_t sum; ///< checksum
  268. } protocolCommon_t;

  269. /** 4.3 The device MCU informs the WiFi module of the configuration mode protocol structure */
  270. typedef struct
  271. {
  272. protocolHead_t head; ///< Protocol standard header structure
  273. uint8_t cfgMode; ///< Configuration parameters
  274. uint8_t sum; ///< checksum
  275. } protocolCfgMode_t;

  276. /** 4.13 The MCU requests the network time protocol structure */
  277. typedef struct
  278. {
  279. protocolHead_t head; ///< Protocol standard header structure
  280. uint8_t time[7]; ///< Hardware version
  281. uint8_t ntp_time[4]; ///< Software version
  282. uint8_t sum; ///< checksum
  283. } protocolUTT_t;

  284. /** WiFi module working status*/
  285. typedef union
  286. {
  287. uint16_t value;
  288. struct
  289. {
  290. uint16_t softap:1;
  291. uint16_t station:1;
  292. uint16_t onboarding:1;
  293. uint16_t binding:1;
  294. uint16_t con_route:1;
  295. uint16_t con_m2m:1;
  296. uint16_t reserve1:2;
  297. uint16_t rssi:3;
  298. uint16_t app:1;
  299. uint16_t test:1;
  300. uint16_t reserve2:3;
  301. }types;

  302. } wifiStatus_t;

  303. /** WiFi status type :protocol structure */
  304. typedef struct
  305. {
  306. protocolHead_t head; ///< Protocol standard header structure
  307. wifiStatus_t ststus; ///< WIFI status
  308. uint8_t sum; ///< checksum
  309. } protocolWifiStatus_t;

  310. /** Protocol common data frame(4.9) :protocol structure */
  311. typedef struct
  312. {
  313. protocolHead_t head; ///< Protocol standard header structure
  314. uint8_t type; ///< Information Type
  315. uint8_t sum; ///< checksum
  316. } protocolGetModuleInfo_t;

  317. typedef struct
  318. {
  319. uint8_t moduleType; ///< Information Type
  320. uint8_t serialVer[8]; ///< Serial port protocol version
  321. uint8_t hardVer[8]; ///< Hardware version
  322. uint8_t softVer[8]; ///< Software version
  323. uint8_t mac[16]; ///< mac
  324. uint8_t ip[16]; ///< ip
  325. uint8_t devAttr[8]; ///< Device attribute
  326. } moduleInfo_t;

  327. /** Protocol common data frame(4.9) :protocol structure */
  328. typedef struct
  329. {
  330. protocolHead_t head; ///< Protocol standard header structure
  331. moduleInfo_t wifiModuleInfo; ///< WIFI module information
  332. uint8_t sum; ///< checksum
  333. } protocolModuleInfo_t;


  334. /** GPRS information of base station */
  335. typedef struct
  336. {
  337. uint16_t LAC_ID; ///
  338. uint16_t CellID; ///
  339. uint8_t RSSI; ///
  340. } gprsCellInfo_t;


  341. /** 3.19 The basic information of the GPRS communication module */
  342. typedef struct
  343. {
  344. uint8_t Type;//2G/3g/4g
  345. uint8_t Pro_ver[8];//Universal serial port protocol version
  346. uint8_t Hard_ver[8];//Hardware version
  347. uint8_t Soft_ver[8];//Software version
  348. uint8_t Device_attribute[8];//Device attribute
  349. uint8_t IMEI[16];//string
  350. uint8_t IMSI[16];//string
  351. uint8_t MCC[8];//Mobile country code
  352. uint8_t MNC[8];//Mobile network code
  353. uint8_t CellNum;//Number of base station
  354. uint8_t CellInfoLen;//Information length of base station
  355. gprsCellInfo_t GPRS_CellINFO[CELLNUMMAX];
  356. }gprsInfo_t;

  357. /** 4.7 Illegal message notification :protocol structure*/
  358. typedef struct
  359. {
  360. protocolHead_t head; ///< Protocol standard header structure
  361. uint8_t error; ///< error value
  362. uint8_t sum; ///< checksum
  363. } protocolErrorType_t;


  364. /** P0 message header */
  365. typedef struct
  366. {
  367. protocolHead_t head; ///< Protocol standard header structure
  368. uint8_t action; ///< p0 command
  369. } protocolP0Head_t;


  370. /** protocol “4.9 The device MCU reports the current status to the WiFi module” device status "dev_status(11B)" */
  371. typedef struct {

  372. devStatus_t devStatus; ///< Stores the device status data
  373. }gizwitsReport_t;

  374. /** resend strategy structure */
  375. typedef struct {
  376. uint8_t num; ///< resend times
  377. 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
  378. uint8_t buf[MAX_PACKAGE_LEN]; ///< resend data buffer
  379. uint16_t dataLen; ///< resend data length
  380. uint32_t sendTime; ///< resend time
  381. } protocolWaitAck_t;

  382. /** 4.8 WiFi read device datapoint value , device ack use this struct */
  383. typedef struct
  384. {
  385. protocolHead_t head; ///< Protocol head
  386. uint8_t action; ///< p0 action
  387. gizwitsReport_t reportData; ///< p0 data
  388. uint8_t sum; ///< Checksum
  389. } protocolReport_t;


  390. /** Protocol main and very important struct */
  391. typedef struct
  392. {
  393. uint8_t issuedFlag; ///< P0 action type
  394. uint8_t protocolBuf[MAX_PACKAGE_LEN]; ///< Protocol data handle buffer
  395. uint8_t transparentBuff[MAX_PACKAGE_LEN]; ///< Transparent data storage area
  396. uint32_t transparentLen; ///< Transmission data length

  397. uint32_t sn; ///< Message SN
  398. uint32_t timerMsCount; ///< Timer Count
  399. protocolWaitAck_t waitAck; ///< Protocol wait ACK data structure

  400. eventInfo_t issuedProcessEvent; ///< Control events
  401. eventInfo_t wifiStatusEvent; ///< WIFI Status events
  402. eventInfo_t NTPEvent; ///< NTP events
  403. eventInfo_t moduleInfoEvent; ///< Module Info events

  404. gizwitsReport_t reportData; ///< The protocol reports data for standard product
  405. dataPoint_t gizCurrentDataPoint; ///< Current device datapoints status
  406. dataPoint_t gizLastDataPoint; ///< Last device datapoints status
  407. moduleStatusInfo_t wifiStatusData; ///< WIFI signal intensity
  408. protocolTime_t TimeNTP; ///< Network time information
  409. #if MODULE_TYPE
  410. gprsInfo_t gprsInfoNews;
  411. #else
  412. moduleInfo_t wifiModuleNews; ///< WIFI module Info
  413. #endif


  414. }gizwitsProtocol_t;

  415. #pragma pack()

  416. /**@name Gizwits user API interface
  417. * @{
  418. */

  419. extern uint32_t gizGetTimerCount(void);

  420. void gizwitsInit(void);
  421. int32_t gizwitsSetMode(uint8_t mode);
  422. void gizwitsGetNTP(void);
  423. int32_t gizwitsHandle(dataPoint_t *currentData);
  424. int32_t gizwitsPassthroughData(uint8_t * gizdata, uint32_t len);
  425. void gizwitsGetModuleInfo(void);
  426. int32_t gizPutData(uint8_t *buf, uint32_t len);


  427. /*添加用户自定义的函数**/
  428. void gziwits_Task(dataPoint_t * currentDataPoint);

  429. /**@} */
  430. #ifdef __cplusplus
  431. }
  432. #endif

  433. #endif


复制代码




6,Utils直接移植无需修改

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



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

  1. #include "tim3.h"
  2. #include "gizwits_product.h"

  3. void TIM3_Config(uint16_t psc,uint16_t arr)
  4. {
  5. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  6. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  7. TIM_TimeBaseStructure.TIM_Period = arr;
  8. TIM_TimeBaseStructure.TIM_Prescaler = psc;
  9. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  10. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  11. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  12. TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );

  13. NVIC_InitTypeDef NVIC_InitStructure;
  14. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  15. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  16. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  17. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  18. NVIC_Init(&NVIC_InitStructure);
  19. TIM_Cmd(TIM3, ENABLE);
  20. }
  21. /*用户实现的定时器接口*/
  22. void TIM3_IRQHandler(void)
  23. {
  24. if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  25. {
  26. TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
  27. gizTimerMs();
  28. }
  29. }
  30. /*******************/
  31. //void TIMER_IRQ_FUN(void)
  32. //{
  33. // gizTimerMs();
  34. //}
  35. /*******************/


  36. ## 添加UART3串口通信代码驱动

  37. ```c
  38. #include "usart3.h"
  39. #include "gizwits_product.h"

  40. void USART3_Init(uint32_t BaudRate)
  41. {
  42. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //GPIOB时钟
  43. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口3时钟使能

  44. USART_DeInit(USART3); //复位串口3
  45. GPIO_InitTypeDef GPIO_InitStructure;


  46. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
  47. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //USART3_TX PB10
  48. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  49. GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB10


  50. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //USART3_RX PB11
  51. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
  52. GPIO_Init(GPIOB, &GPIO_InitStructure);

  53. USART_InitTypeDef USART_InitStructure;
  54. USART_InitStructure.USART_BaudRate = BaudRate; //波特率一般设置为9600;
  55. USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
  56. USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
  57. USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
  58. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  59. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式

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


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


  62. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //开启中断

  63. NVIC_InitTypeDef NVIC_InitStructure;
  64. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  65. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  66. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  67. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  68. NVIC_Init(&NVIC_InitStructure);

  69. }
  70. /*用户实现的中断服务函数接口*/
  71. void USART3_IRQHandler(void)
  72. {
  73. uint8_t Recv_Data;
  74. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//接收到数据
  75. {
  76. Recv_Data = USART_ReceiveData(USART3);
  77. gizPutData(&Recv_Data, 1);
  78. }
  79. }
  80. /*********************/
  81. //void UART_IRQ_FUN(void)
  82. //{
  83. // uint8_t value = 0;
  84. // gizPutData(&value, 1);
  85. //}
  86. /*********************/

复制代码



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


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

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



设备上线显示

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



效果展示

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

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章