version 1.1.2.3, 2012/05/01 01:12:16
|
version 1.1.2.4, 2012/05/08 09:07:31
|
Line 50 SUCH DAMAGE.
|
Line 50 SUCH DAMAGE.
|
* mqtt_cli_Open() - Open client connection to MQTT broker |
* mqtt_cli_Open() - Open client connection to MQTT broker |
* |
* |
* @addr = brokers address |
* @addr = brokers address |
|
* @timeout = timeout |
* return: NULL error or !=NULL connected to broker |
* return: NULL error or !=NULL connected to broker |
*/ |
*/ |
mqtt_cli_t * |
mqtt_cli_t * |
mqtt_cli_Open(struct sockaddr *addr) | mqtt_cli_Open(struct sockaddr *addr, u_short timeout) |
{ |
{ |
mqtt_cli_t *cli; |
mqtt_cli_t *cli; |
|
|
Line 67 mqtt_cli_Open(struct sockaddr *addr)
|
Line 68 mqtt_cli_Open(struct sockaddr *addr)
|
} else |
} else |
memset(cli, 0, sizeof(mqtt_cli_t)); |
memset(cli, 0, sizeof(mqtt_cli_t)); |
|
|
|
cli->timeout = timeout; |
cli->sock = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP); |
cli->sock = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP); |
if (cli->sock == -1) { |
if (cli->sock == -1) { |
LOGERR; |
LOGERR; |
Line 118 mqtt_cli_Close(mqtt_cli_t ** __restrict cli)
|
Line 120 mqtt_cli_Close(mqtt_cli_t ** __restrict cli)
|
free(*cli); |
free(*cli); |
*cli = NULL; |
*cli = NULL; |
return 0; |
return 0; |
|
} |
|
|
|
/* |
|
* mqtt_cli_Subscribe() - Subscribe to broker |
|
* |
|
* @cli = connected client |
|
* @Topics = Topics for subscribes |
|
* @msgID = Message ID |
|
* @Dup = Duplicated request |
|
* @QoS = Message QoS |
|
* return: NULL error or !=NULL allocated array with subscribed QoS responses, |
|
* must be free() result! |
|
*/ |
|
u_char * |
|
mqtt_cli_Subscribe(mqtt_cli_t * __restrict cli, mqtt_subscr_t * __restrict Topics, |
|
u_short msgID, u_char Dup, u_char QoS) |
|
{ |
|
int siz = 0; |
|
u_short mid = 0; |
|
u_char *qoses = NULL; |
|
|
|
if (!cli) |
|
return NULL; |
|
|
|
/* send subscribe */ |
|
siz = mqtt_msgSUBSCRIBE(cli->buf, Topics, msgID, Dup, QoS); |
|
if (siz == -1) |
|
return NULL; |
|
siz = send(cli->sock, cli->buf->msg_base, siz, MSG_NOSIGNAL); |
|
if (siz == -1) { |
|
LOGERR; |
|
return NULL; |
|
} |
|
|
|
if ((siz = mqtt_wait4data(cli->sock, cli->timeout, POLLIN | POLLPRI)) == -1) { |
|
return NULL; |
|
} else if (siz && mqtt_KeepAlive(cli->sock, cli->timeout, 1)) |
|
return NULL; |
|
|
|
/* receive suback */ |
|
siz = recv(cli->sock, cli->buf->msg_base, cli->buf->msg_len, 0); |
|
if (siz == -1) { |
|
LOGERR; |
|
return NULL; |
|
} |
|
siz = mqtt_readSUBACK(cli->buf, &mid, &qoses); |
|
if (siz == -1) |
|
return NULL; |
|
if (msgID != mid) { |
|
free(qoses); |
|
mqtt_SetErr(EBADMSG, "Receive different message ID %hu != %hu", msgID, mid); |
|
return NULL; |
|
} |
|
|
|
return qoses; |
} |
} |