01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
int
transport_open(
char
* addr,
int
port)
{
int
* sock = &mysock;
struct
hostent *server;
struct
sockaddr_in serv_addr;
static
struct
timeval tv;
int
timeout = 1000;
fd_set readset;
fd_set writeset;
*sock = socket(AF_INET, SOCK_STREAM, 0);
if
(*sock < 0)
DiagPrintf(
"[ERROR] Create socket failed\n"
);
server = gethostbyname(addr);
if
(server == NULL)
DiagPrintf(
"[ERROR] Get host ip failed\n"
);
memset
(&serv_addr,0,
sizeof
(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
memcpy
(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);
if
(connect(*sock,(
struct
sockaddr *)&serv_addr,
sizeof
(serv_addr)) < 0){
DiagPrintf(
"[ERROR] connect failed\n"
);
return
-1;
}
tv.tv_sec = 10;
/* 1 second Timeout */
tv.tv_usec = 0;
setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (
char
*)&timeout,
sizeof
(timeout));
return
mysock;
}
|
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#define HOST_NAME "m2m.eclipse.org"[/align]#define HOST_PORT 1883
void
mqtt_thread(
void
*arg)
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
MQTTString receivedTopic;
int
rc = 0;
char
buf[200];
int
buflen =
sizeof
(buf);
int
mysock = 0;
MQTTString topicString = MQTTString_initializer;
int
payloadlen_in;
unsigned
char
* payload_in;
unsigned
short
msgid = 1;
int
subcount;
int
granted_qos =0;
unsigned
char
sessionPresent, connack_rc;
unsigned
short
submsgid;
int
len = 0;
int
req_qos = 1;
unsigned
char
dup;
int
qos;
unsigned
char
retained;
char
*host =
"m2m.eclipse.org"
;
int
port = 1883;
uint8_t msgtypes = CONNECT;
uint32_t curtick = xTaskGetTickCount();
log_info(
"socket connect to server"
);
mysock = transport_open(host,port);
if
(mysock < 0)
return
mysock;
log_notice(
"Sending to hostname %s port %d\n"
, host, port);
data.clientID.cstring =
"me"
;
data.keepAliveInterval = 50;
data.cleansession = 1;
data.username.cstring =
""
;
data.password.cstring =
""
;
data.MQTTVersion = 4;
while
(1)
{
if
((xTaskGetTickCount() - curtick) >(data.keepAliveInterval/2*1000))
{
if
(msgtypes == 0)
{
curtick = xTaskGetTickCount();
msgtypes = PINGREQ;
}
}
switch
(msgtypes)
{
case
CONNECT: len = MQTTSerialize_connect(buf, buflen, &data);
rc = transport_sendPacketBuffer(mysock, (unsigned
char
*)buf, len);
if
(rc == len)
log_info(
"send CONNECT Successfully"
);
else
log_debug(
"send CONNECT failed"
);
log_info(
"MQTT concet to server!"
);
msgtypes = 0;
break
;
case
CONNACK:
if
(MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
{
log_info(
"Unable to connect, return code %d\n"
, connack_rc);
}
else
log_info(
"MQTT is concet OK!"
);
msgtypes = SUBSCRIBE;
break
;
case
SUBSCRIBE: topicString.cstring =
"ledtest"
;
len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);
rc = transport_sendPacketBuffer(mysock, (unsigned
char
*)buf, len);
if
(rc == len)
log_info(
"send SUBSCRIBE Successfully\n"
);
else
log_debug(
"send SUBSCRIBE failed\n"
);
log_info(
"client subscribe:[%s]"
,topicString.cstring);
msgtypes = 0;
break
;
case
SUBACK: rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
log_info(
"granted qos is %d\n"
, granted_qos);
msgtypes = 0;
break
;
case
PUBLISH: rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, &payload_in, &payloadlen_in, buf, buflen);
log_info(
"message arrived : %s\n"
, payload_in);
if
(
strstr
(payload_in,
"on"
))
{
log_notice(
"LED on!!"
);
}
else
if
(
strstr
(payload_in,
"off"
))
{
log_notice(
"LED off!!"
);
}
if
(qos == 1)
{
log_info(
"publish qos is 1,send publish ack."
);
memset
(buf,0,buflen);
len = MQTTSerialize_ack(buf,buflen,PUBACK,dup,msgid);
//publish ack
rc = transport_sendPacketBuffer(mysock, (unsigned
char
*)buf, len);
if
(rc == len)
log_info(
"send PUBACK Successfully"
);
else
log_debug(
"send PUBACK failed"
);
}
msgtypes = 0;
break
;
case
PUBACK: log_info(
"PUBACK!"
);
msgtypes = 0;
break
;
case
PUBREC: log_info(
"PUBREC!"
);
//just for qos2
break
;
case
PUBREL: log_info(
"PUBREL!"
);
//just for qos2
break
;
case
PUBCOMP: log_info(
"PUBCOMP!"
);
//just for qos2
break
;
case
PINGREQ: len = MQTTSerialize_pingreq(buf, buflen);
rc = transport_sendPacketBuffer(mysock, (unsigned
char
*)buf, len);
if
(rc == len)
log_info(
"send PINGREQ Successfully\n"
);
else
log_debug(
"send PINGREQ failed\n"
);
log_info(
"time to ping mqtt server to take alive!"
);
msgtypes = 0;
break
;
case
PINGRESP: log_info(
"mqtt server Pong"
);
msgtypes = 0;
break
;
}
memset
(buf,0,buflen);
rc=MQTTPacket_read(buf, buflen, transport_getdata);
if
(rc >0)
{
msgtypes = rc;
log_info(
"MQTT is get recv:"
);
}
gpio_write(&gpio_led, !gpio_read(&gpio_led));
}
exit
:
transport_close(mysock);
log_info(
"mqtt thread exit."
);
vTaskDelete(NULL);
}
|