Skip to content

Commit 432c36f

Browse files
authored
Update self_mqtt.cc
1 parent 6054f76 commit 432c36f

File tree

1 file changed

+66
-74
lines changed

1 file changed

+66
-74
lines changed

arc_design_contest/2021/HUST_SmartMeter/src/self_mqtt.cc

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,19 @@ static void Init(uint8_t *prx,uint16_t rxlen,uint8_t *ptx,uint16_t txlen)
7171
//连接服务器的打包函数
7272
static uint8_t Connect(char *ClientID,char *Username,char *Password)
7373
{
74-
int ClientIDLen = strlen(ClientID);
75-
int UsernameLen = strlen(Username);
76-
int PasswordLen = strlen(Password);
77-
int DataLen;
74+
int ClientIDLen = strlen(ClientID);
75+
int UsernameLen = strlen(Username);
76+
int PasswordLen = strlen(Password);
77+
int DataLen;
7878
_mqtt.txlen=0;
7979
//可变报头+Payload 每个字段包含两个字节的长度标识
80-
DataLen = 10 + (ClientIDLen+2) + (UsernameLen+2) + (PasswordLen+2);
80+
DataLen = 10 + (ClientIDLen+2) + (UsernameLen+2) + (PasswordLen+2);
8181

8282
//固定报头
8383
//控制报文类型
84-
_mqtt.txbuf[_mqtt.txlen++] = 0x10; //MQTT Message Type CONNECT
84+
_mqtt.txbuf[_mqtt.txlen++] = 0x10; //MQTT Message Type CONNECT
8585
//剩余长度(不包括固定头部)
86-
do
87-
{
86+
do{
8887
uint8_t encodedByte = DataLen % 128;
8988
DataLen = DataLen / 128;
9089
// if there are more data to encode, set the top bit of this byte
@@ -95,44 +94,41 @@ static uint8_t Connect(char *ClientID,char *Username,char *Password)
9594

9695
//可变报头
9796
//协议名
98-
_mqtt.txbuf[_mqtt.txlen++] = 0; // Protocol Name Length MSB
99-
_mqtt.txbuf[_mqtt.txlen++] = 4; // Protocol Name Length LSB
100-
_mqtt.txbuf[_mqtt.txlen++] = 'M'; // ASCII Code for M
101-
_mqtt.txbuf[_mqtt.txlen++] = 'Q'; // ASCII Code for Q
102-
_mqtt.txbuf[_mqtt.txlen++] = 'T'; // ASCII Code for T
103-
_mqtt.txbuf[_mqtt.txlen++] = 'T'; // ASCII Code for T
97+
_mqtt.txbuf[_mqtt.txlen++] = 0; // Protocol Name Length MSB
98+
_mqtt.txbuf[_mqtt.txlen++] = 4; // Protocol Name Length LSB
99+
_mqtt.txbuf[_mqtt.txlen++] = 'M'; // ASCII Code for M
100+
_mqtt.txbuf[_mqtt.txlen++] = 'Q'; // ASCII Code for Q
101+
_mqtt.txbuf[_mqtt.txlen++] = 'T'; // ASCII Code for T
102+
_mqtt.txbuf[_mqtt.txlen++] = 'T'; // ASCII Code for T
104103
//协议级别
105-
_mqtt.txbuf[_mqtt.txlen++] = 4; // MQTT Protocol version = 4
104+
_mqtt.txbuf[_mqtt.txlen++] = 4; // MQTT Protocol version = 4
106105
//连接标志
107-
_mqtt.txbuf[_mqtt.txlen++] = 0xc2; // conn flags
108-
_mqtt.txbuf[_mqtt.txlen++] = 0; // Keep-alive Time Length MSB
109-
_mqtt.txbuf[_mqtt.txlen++] = 60; // Keep-alive Time Length LSB 60S������
106+
_mqtt.txbuf[_mqtt.txlen++] = 0xc2; // conn flags
107+
_mqtt.txbuf[_mqtt.txlen++] = 0; // Keep-alive Time Length MSB
108+
_mqtt.txbuf[_mqtt.txlen++] = 60; // Keep-alive Time Length LSB 60S������
110109

111-
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(ClientIDLen);// Client ID length MSB
112-
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(ClientIDLen);// Client ID length LSB
110+
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(ClientIDLen);// Client ID length MSB
111+
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(ClientIDLen);// Client ID length LSB
113112
memcpy(&_mqtt.txbuf[_mqtt.txlen],ClientID,ClientIDLen);
114-
_mqtt.txlen += ClientIDLen;
113+
_mqtt.txlen += ClientIDLen;
115114

116-
if(UsernameLen > 0)
117-
{
118-
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(UsernameLen); //username length MSB
119-
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(UsernameLen); //username length LSB
115+
if(UsernameLen > 0){
116+
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(UsernameLen); //username length MSB
117+
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(UsernameLen); //username length LSB
120118
memcpy(&_mqtt.txbuf[_mqtt.txlen],Username,UsernameLen);
121-
_mqtt.txlen += UsernameLen;
122-
}
119+
_mqtt.txlen += UsernameLen;
120+
}
123121

124-
if(PasswordLen > 0)
125-
{
126-
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(PasswordLen); //password length MSB
127-
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(PasswordLen); //password length LSB
122+
if(PasswordLen > 0){
123+
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(PasswordLen); //password length MSB
124+
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(PasswordLen); //password length LSB
128125
memcpy(&_mqtt.txbuf[_mqtt.txlen],Password,PasswordLen);
129-
_mqtt.txlen += PasswordLen;
130-
}
126+
_mqtt.txlen += PasswordLen;
127+
}
131128

132129
uint8_t cnt=10;
133130
uint8_t wait;
134-
while(cnt--)
135-
{
131+
while(cnt--){
136132
UART_RX_STA=0;
137133
memset(_mqtt.rxbuf,0,_mqtt.rxlen);
138134
Mqtt_SendBuf((char *)_mqtt.txbuf,_mqtt.txlen);
@@ -163,16 +159,16 @@ static uint8_t Connect(char *ClientID,char *Username,char *Password)
163159
static uint8_t SubscribeTopic(char *topic,uint8_t qos,uint8_t whether)
164160
{
165161
_mqtt.txlen=0;
166-
int topiclen = strlen(topic);
162+
int topiclen = strlen(topic);
167163

168164
int DataLen = 2 + (topiclen+2) + (whether?1:0);//可变报头的长度(2字节)加上有效载荷的长度
169165
//固定报头
170166
//控制报文类型
171-
if(whether) _mqtt.txbuf[_mqtt.txlen++] = 0x82; //消息类型和标志订阅
172-
else _mqtt.txbuf[_mqtt.txlen++] = 0xA2; //取消订阅
173-
174-
do
175-
{
167+
if(whether)
168+
_mqtt.txbuf[_mqtt.txlen++] = 0x82; //消息类型和标志订阅
169+
else
170+
_mqtt.txbuf[_mqtt.txlen++] = 0xA2; //取消订阅
171+
do{
176172
uint8_t encodedByte = DataLen % 128;
177173
DataLen = DataLen / 128;
178174
// if there are more data to encode, set the top bit of this byte
@@ -182,18 +178,17 @@ static uint8_t SubscribeTopic(char *topic,uint8_t qos,uint8_t whether)
182178
}while ( DataLen > 0 );
183179

184180
//可变报头
185-
_mqtt.txbuf[_mqtt.txlen++] = 0; //消息标识符 MSB
186-
_mqtt.txbuf[_mqtt.txlen++] = 0x01; //消息标识符 LSB
181+
_mqtt.txbuf[_mqtt.txlen++] = 0; //消息标识符 MSB
182+
_mqtt.txbuf[_mqtt.txlen++] = 0x01; //消息标识符 LSB
187183
//有效载荷
188-
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(topiclen);//主题长度 MSB
189-
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(topiclen);//主题长度 LSB
184+
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(topiclen);//主题长度 MSB
185+
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(topiclen);//主题长度 LSB
190186
memcpy(&_mqtt.txbuf[_mqtt.txlen],topic,topiclen);
191-
_mqtt.txlen += topiclen;
187+
_mqtt.txlen += topiclen;
192188

193-
if(whether)
194-
{
195-
_mqtt.txbuf[_mqtt.txlen++] = qos;//QoS级别
196-
}
189+
if(whether){
190+
_mqtt.txbuf[_mqtt.txlen++] = qos;//QoS级别
191+
}
197192

198193
uint8_t cnt=2;
199194
uint8_t wait;
@@ -202,10 +197,8 @@ static uint8_t SubscribeTopic(char *topic,uint8_t qos,uint8_t whether)
202197
memset(_mqtt.rxbuf,0,_mqtt.rxlen);
203198
Mqtt_SendBuf((char *)_mqtt.txbuf,_mqtt.txlen);
204199
wait=30;//等待3s时间
205-
while(wait--)
206-
{
207-
if(_mqtt.rxbuf[0]==parket_subAck[0] && _mqtt.rxbuf[1]==parket_subAck[1]) //订阅成功
208-
{
200+
while(wait--){
201+
if(_mqtt.rxbuf[0]==parket_subAck[0] && _mqtt.rxbuf[1]==parket_subAck[1]){
209202
return 1;//订阅成功
210203
}
211204
board_delay_ms(100,1);
@@ -221,24 +214,24 @@ static uint8_t SubscribeTopic(char *topic,uint8_t qos,uint8_t whether)
221214
//qos 消息等级
222215
static uint8_t PublishData(char *topic, char *message, uint8_t qos)
223216
{
224-
int topicLength = strlen(topic);
225-
int messageLength = strlen(message);
226-
static uint16_t id=0;
217+
int topicLength = strlen(topic);
218+
int messageLength = strlen(message);
219+
static uint16_t id=0;
227220
int DataLen;
228221
_mqtt.txlen=0;
229222
//有效载荷的长度这样计算:用固定报头中的剩余长度字段的值减去可变报头的长度
230223
//QOS为0时没有标识符
231224
//数据长度 主题名 报文标识符 有效载荷
232-
if(qos) DataLen = (2+topicLength) + 2 + messageLength;
233-
else DataLen = (2+topicLength) + messageLength;
234-
235-
//固定报头
225+
if(qos)
226+
DataLen = (2+topicLength) + 2 + messageLength;
227+
else
228+
DataLen = (2+topicLength) + messageLength;
229+
//固定报头
236230
//控制报文类型
237-
_mqtt.txbuf[_mqtt.txlen++] = 0x30; // MQTT Message Type PUBLISH
231+
_mqtt.txbuf[_mqtt.txlen++] = 0x30; // MQTT Message Type PUBLISH
238232

239233
//剩余长度
240-
do
241-
{
234+
do{
242235
uint8_t encodedByte = DataLen % 128;
243236
DataLen = DataLen / 128;
244237
// if there are more data to encode, set the top bit of this byte
@@ -248,22 +241,21 @@ static uint8_t PublishData(char *topic, char *message, uint8_t qos)
248241
}while ( DataLen > 0 );
249242

250243
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(topicLength);//主题长度MSB
251-
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(topicLength);//主题长度LSB
244+
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(topicLength);//主题长度LSB
252245
memcpy(&_mqtt.txbuf[_mqtt.txlen],topic,topicLength);//拷贝主题
253-
_mqtt.txlen += topicLength;
246+
_mqtt.txlen += topicLength;
254247

255248
//报文标识符
256-
if(qos)
257-
{
258-
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(id);
259-
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(id);
260-
id++;
261-
}
249+
if(qos){
250+
_mqtt.txbuf[_mqtt.txlen++] = BYTE1(id);
251+
_mqtt.txbuf[_mqtt.txlen++] = BYTE0(id);
252+
id++;
253+
}
262254
memcpy(&_mqtt.txbuf[_mqtt.txlen],message,messageLength);
263-
_mqtt.txlen += messageLength;
255+
_mqtt.txlen += messageLength;
264256

265257
Mqtt_SendBuf((char*)_mqtt.txbuf,_mqtt.txlen);
266-
return _mqtt.txlen;
258+
return _mqtt.txlen;
267259
}
268260

269261
static void SentHeart(void)

0 commit comments

Comments
 (0)