[STM32] 学完了CANopen,再回头看看机智云的协议,相似性....

[复制链接]
1981|8
 楼主| 一路向北lm 发表于 2018-8-8 08:58 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2018-8-8 09:42 编辑

机智云协议初始化
代码中提取信息:
pRb                 结构体变量
GIZWITS_LOG   打印输出
rbCreate()
memset ()      这俩才是关注重点

  1. void gizwitsInit(void)
  2. {   
  3.     pRb.rbCapacity = RB_MAX_LEN;
  4.     pRb.rbBuff = rbBuf;
  5.   if(0 == rbCreate(&pRb))
  6.         {
  7.                 GIZWITS_LOG("rbCreate Success \n");
  8.         }
  9.         else
  10.         {
  11.                 GIZWITS_LOG("rbCreate Faild \n");
  12.         }
  13.    
  14.     memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));
  15. }






 楼主| 一路向北lm 发表于 2018-8-8 09:06 | 显示全部楼层
本帖最后由 一路向北lm 于 2018-8-8 09:41 编辑

先看这个结构体变量 pRb
找到它的定义处,沿着  rb_t   pRb 找到了归宿,我们的好奇心又来了,机构体内部元素不足为奇,pragma pack是什么鬼?

  1. #pragma pack(1)
  2. typedef struct
  3. {
  4.     size_t rbCapacity;
  5.     uint8_t  *rbHead;
  6.     uint8_t  *rbTail;
  7.     uint8_t  *rbBuff;
  8. }rb_t;
  9. #pragma pack()


 楼主| 一路向北lm 发表于 2018-8-8 09:10 | 显示全部楼层
本帖最后由 一路向北lm 于 2018-8-8 09:41 编辑

关于#pragma pack(n)

解释一:

每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数)。程序员可以通过预编译命令#pragma pack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你要指定的“对齐系数”。

  规则:

  1、数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragma pack指定的数值和这个数据成员自身长度中,比较小的那个进行。

  2、结构(或联合)的整体对齐规则:在数据成员完成各自对齐之后,结构(或联合)本身也要进行对齐,对齐将按照#pragma pack指定的数值和结构(或联合)最大数据成员长度中,比较小的那个进行。

解释二:

n 字节的对齐方式 VC 对结构的存储的特殊处理确实提高 CPU 存储变量的速度,但是有时候也带来 了一些麻烦,我们也屏蔽掉变量默认的对齐方式,自己可以设定变量的对齐方式。 VC 中提供了#pragma pack(n)来设定变量以 n 字节对齐方式。n 字节对齐就是说 变量存放的起始地址的偏移量有两种情况:

第一、如果 n 大于等于该变量所占用的字 节数,那么偏移量必须满足默认的对齐方式。

第二、如果 n 小于该变量的类型所占用 的字节数,那么偏移量为 n 的倍数,不用满足默认的对齐方式。结构的总大小也有个 约束条件,分下面两种情况:如果 n 大于所有成员变量类型所占用的字节数,那么结 构的总大小必须为占用空间最大的变量占用的空间数的倍数; 否则必须为 n 的倍数。


 楼主| 一路向北lm 发表于 2018-8-8 09:32 | 显示全部楼层
本帖最后由 一路向北lm 于 2018-8-8 09:40 编辑

接着看 rbCreate(),目前为止还不知道干嘛用,至于rbHead  rbTail 大家一看便懂。
  1. int8_t ICACHE_FLASH_ATTR rbCreate(rb_t* rb)
  2. {
  3.     if(NULL == rb)
  4.     {
  5.         return -1;
  6.     }

  7.     rb->rbHead = rb->rbBuff;
  8.     rb->rbTail = rb->rbBuff;
  9.     return 0;
  10. }


 楼主| 一路向北lm 发表于 2018-8-8 09:36 | 显示全部楼层
本帖最后由 一路向北lm 于 2018-8-8 09:40 编辑

Memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;
memset()函数原型是extern void *memset(void *buffer, int c, int count)      
buffer:为指针或是数组, c:是赋给buffer的值,count:是buffer的长度.

这个函数在socket中多用于清空数组.如:原型是memset(buffer, 0, sizeof(buffer))

这段也就不难理解了: memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));

将机智云的机构体清零做初始化。


 楼主| 一路向北lm 发表于 2018-8-8 09:43 | 显示全部楼层
看下机智云用于通讯的结构体,参数还是很多的,以后慢慢研究。
  1. typedef struct
  2. {
  3.     uint8_t issuedFlag;                             ///< P0 action type
  4.     uint8_t protocolBuf[MAX_PACKAGE_LEN];           ///< Protocol data handle buffer
  5.     uint8_t transparentBuff[MAX_PACKAGE_LEN];       ///< Transparent data storage area
  6.     uint32_t transparentLen;                        ///< Transmission data length
  7.    
  8.     uint32_t sn;                                    ///< Message SN
  9.     uint32_t timerMsCount;                          ///< Timer Count
  10.     protocolWaitAck_t waitAck;                      ///< Protocol wait ACK data structure
  11.    
  12.     eventInfo_t issuedProcessEvent;                 ///< Control events
  13.     eventInfo_t wifiStatusEvent;                    ///< WIFI Status events
  14.     eventInfo_t NTPEvent;                           ///< NTP events
  15.     eventInfo_t moduleInfoEvent;                    ///< Module Info events

  16.           gizwitsReport_t reportData;                     ///< The protocol reports data for standard product
  17.     dataPoint_t gizCurrentDataPoint;                ///< Current device datapoints status
  18.     dataPoint_t gizLastDataPoint;                   ///< Last device datapoints status
  19.     moduleStatusInfo_t wifiStatusData;              ///< WIFI signal intensity
  20.     protocolTime_t TimeNTP;                         ///< Network time information
  21. #if MODULE_TYPE
  22.     gprsInfo_t   gprsInfoNews;
  23. #else  
  24.     moduleInfo_t  wifiModuleNews;                   ///< WIFI module Info
  25. #endif
  26.         
  27.         
  28. }gizwitsProtocol_t;


 楼主| 一路向北lm 发表于 2018-8-8 09:46 | 显示全部楼层
下面就是关于对WIFI模式选择的函数,很长,大家都不想看,我们来找重点,抓住核心。
  1. int32_t gizwitsSetMode(uint8_t mode)
  2. {
  3.     int32_t ret = 0;
  4.     protocolCfgMode_t cfgMode;
  5.     protocolCommon_t setDefault;

  6.     switch(mode)
  7.     {
  8.         case WIFI_RESET_MODE:
  9.             gizProtocolHeadInit((protocolHead_t *)&setDefault);
  10.             setDefault.head.cmd = CMD_SET_DEFAULT;
  11.             setDefault.head.sn = gizwitsProtocol.sn++;
  12.             setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  13.             setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  14.             ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  15.             if(ret < 0)
  16.             {
  17.                 GIZWITS_LOG("ERR: uart write error %d \n", ret);
  18.             }

  19.             gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));   
  20.             break;
  21.         case WIFI_SOFTAP_MODE:
  22.             gizProtocolHeadInit((protocolHead_t *)&cfgMode);
  23.             cfgMode.head.cmd = CMD_WIFI_CONFIG;
  24.             cfgMode.head.sn = gizwitsProtocol.sn++;
  25.             cfgMode.cfgMode = mode;
  26.             cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);
  27.             cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  28.             ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  29.             if(ret < 0)
  30.             {
  31.                 GIZWITS_LOG("ERR: uart write error %d \n", ret);
  32.             }
  33.             gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  34.             break;
  35.         case WIFI_AIRLINK_MODE:
  36.             gizProtocolHeadInit((protocolHead_t *)&cfgMode);
  37.             cfgMode.head.cmd = CMD_WIFI_CONFIG;
  38.             cfgMode.head.sn = gizwitsProtocol.sn++;
  39.             cfgMode.cfgMode = mode;
  40.             cfgMode.head.len = exchangeBytes(sizeof(protocolCfgMode_t)-4);
  41.             cfgMode.sum = gizProtocolSum((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  42.             ret = uartWrite((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  43.             if(ret < 0)
  44.             {
  45.                 GIZWITS_LOG("ERR: uart write error %d \n", ret);
  46.             }
  47.             gizProtocolWaitAck((uint8_t *)&cfgMode, sizeof(protocolCfgMode_t));
  48.             break;
  49.         case WIFI_PRODUCTION_TEST:
  50.             gizProtocolHeadInit((protocolHead_t *)&setDefault);
  51.             setDefault.head.cmd = CMD_PRODUCTION_TEST;
  52.             setDefault.head.sn = gizwitsProtocol.sn++;
  53.             setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  54.             setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  55.             ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  56.             if(ret < 0)
  57.             {
  58.                 GIZWITS_LOG("ERR: uart write error %d \n", ret);
  59.             }

  60.             gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  61.             break;
  62.         case WIFI_NINABLE_MODE:
  63.             gizProtocolHeadInit((protocolHead_t *)&setDefault);
  64.             setDefault.head.cmd = CMD_NINABLE_MODE;
  65.             setDefault.head.sn = gizwitsProtocol.sn++;
  66.             setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  67.             setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  68.             ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  69.             if(ret < 0)
  70.             {
  71.                 GIZWITS_LOG("ERR: uart write error %d \n", ret);
  72.             }

  73.             gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  74.             break;
  75.         case WIFI_REBOOT_MODE:
  76.             gizProtocolHeadInit((protocolHead_t *)&setDefault);
  77.             setDefault.head.cmd = CMD_REBOOT_MODULE;
  78.             setDefault.head.sn = gizwitsProtocol.sn++;
  79.             setDefault.head.len = exchangeBytes(sizeof(protocolCommon_t)-4);
  80.             setDefault.sum = gizProtocolSum((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  81.             ret = uartWrite((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  82.             if(ret < 0)
  83.             {
  84.                 GIZWITS_LOG("ERR: uart write error %d \n", ret);
  85.             }

  86.             gizProtocolWaitAck((uint8_t *)&setDefault, sizeof(protocolCommon_t));
  87.             break;
  88.         default:
  89.             GIZWITS_LOG("ERR: CfgMode error!\n");
  90.             break;
  91.     }

  92.     return ret;
  93. }


 楼主| 一路向北lm 发表于 2018-8-8 09:57 | 显示全部楼层
先来简单捋一捋,看下有几种模式
1.WIFI_RESET_MODE  :  恢复模组出厂配置接口, 调用会清空所有配置参数, 恢复到出厂默认配置。  

2.WIFI_SOFTAP_MODE:  配置模式切换接口,配置模组进入 SoftAp 模式。
3.WIFI_AIRLINK_MODE: 配置模式切换接口,配置模组进入 AirLink 模式。
4.WIFI_PRODUCTION_TEST:      模组进入产测模式。
5.WIFI_NINABLE_MODE:             模组进入可绑定模式。
6.WIFI_REBOOT_MODE:               重启模式。
 楼主| 一路向北lm 发表于 2018-8-8 10:03 | 显示全部楼层
WIFI_SOFTAP_MODE  AP模式:
Soft AP模式是一种通过无线网卡,使用专用软件在PC上实现AP功能的技术,它可以取代无线网络中的AP(Access Point,无线接入点),从而会降低无线组网的成本。也就是可以把载体作为无线接入点,让电脑、手机或者其他上网设备的无线网连接到载体上,然后通过载体的网(GPRS或者3G)上网,简单的说就是有点共享模式的意思。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部