File:  [ELWIX - Embedded LightWeight unIX -] / libaitmqtt / example / cmds.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Jun 20 15:02:23 2012 UTC (12 years ago) by misho
Branches: MAIN
CVS tags: mqtt1_2, MQTT1_1, HEAD
version 1.1

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <string.h>
    4: #include <sys/types.h>
    5: #include <aitmqtt.h>
    6: 
    7: 
    8: int
    9: main()
   10: {
   11: 	mqtt_msg_t *m;
   12: 	mqtt_subscr_t s[4], *ss;
   13: 	mqtthdr_connflgs_t flg;
   14: 	mqtthdr_connack_t cack;
   15: 	u_short ka, msgID;
   16: 	int i, len;
   17: 	char cid[BUFSIZ], user[BUFSIZ], pass[BUFSIZ], topic[BUFSIZ], *t, *msg, *pub;
   18: 	struct mqtthdr *hdr;
   19: 	u_char *qoses;
   20: 
   21: 	m = mqtt_msgAlloc(0);
   22: 	/* conn* */
   23: 	printf("connect=%d/%d\n", m->msg_len, mqtt_msgCONNECT(m, "MRYN", 0, "aaaaa", NULL, "bbb", NULL, 0, 0, 0));
   24: 	for (i = 0; i < m->msg_len; i++)
   25: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   26: 	t = msg = NULL;
   27: 	cack = mqtt_readCONNECT(m, &ka, cid, sizeof cid, user, sizeof user, pass, sizeof pass, &t, &msg);
   28: 	flg.flags = cack.reserved;
   29: 	printf("read connect %d flags:: clean=%d will=%d qos=%d retain=%d pass=%d user=%d\n", cack.retcode, 
   30: 			flg.clean_sess, flg.will_flg, flg.will_qos, flg.will_retain, flg.password, flg.username);
   31: 	if (flg.reserved) {
   32: 		printf("Error:: mqtt_readCONNECT() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   33: 		return 1;
   34: 	}
   35: 	printf("++> KA=%d sec, ConnID=%s User=%s Pass=%s Will_Topic=%s Will_Message=%s\n", ka, 
   36: 			cid, user, pass, t, msg);
   37: 	if (t)
   38: 		free(t);
   39: 	if (msg)
   40: 		free(msg);
   41: 	printf("connack=%d/%d\n", m->msg_len, mqtt_msgCONNACK(m, 1));
   42: 	for (i = 0; i < m->msg_len; i++)
   43: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   44: 	printf("read connack=%d\n", mqtt_readCONNACK(m));
   45: 	getchar();
   46: 
   47: 	/* pub* */
   48: 	printf("publish=%d/%d\n", m->msg_len, mqtt_msgPUBLISH(m, "AAA/bbb/CCC/ddd", 7, 0, 2, 0, "OLE!!!", 7));
   49: 	for (i = 0; i < m->msg_len; i++)
   50: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   51: 	if (!(len = mqtt_readPUBLISH(m, topic, sizeof topic, &msgID, (void**) &pub))) {
   52: 		printf("Error:: mqtt_readPUBLISH() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   53: 		return 2;
   54: 	}
   55: 	printf("read publish: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
   56: 	printf("++> topic=%s MessageID=%d DATA=(%d)%s\n", topic, msgID, len, pub);
   57: 	free(pub);
   58: 	printf("puback=%d/%d\n", m->msg_len, mqtt_msgPUBACK(m, 10));
   59: 	for (i = 0; i < m->msg_len; i++)
   60: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   61: 	printf("read puback=%d\n", mqtt_readPUBACK(m));
   62: 	printf("pubrec=%d/%d\n", m->msg_len, mqtt_msgPUBREC(m, 11));
   63: 	for (i = 0; i < m->msg_len; i++)
   64: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   65: 	printf("read pubrec=%d\n", mqtt_readPUBREC(m));
   66: 	printf("pubrel=%d/%d\n", m->msg_len, mqtt_msgPUBREL(m, 12));
   67: 	for (i = 0; i < m->msg_len; i++)
   68: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   69: 	printf("read pubrel=%d\n", mqtt_readPUBREL(m));
   70: 	printf("pubcomp=%d/%d\n", m->msg_len, mqtt_msgPUBCOMP(m, 13));
   71: 	for (i = 0; i < m->msg_len; i++)
   72: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   73: 	printf("read pubcomp=%d\n", mqtt_readPUBCOMP(m));
   74: 	getchar();
   75: 
   76: 	/* sub* */
   77: 	memset(s, 0, sizeof s);
   78: 	s[0].sub_topic.msg_len = 3;
   79: 	s[0].sub_topic.msg_base = "a/b";
   80: 	s[0].sub_ret = MQTT_QOS_ACK;
   81: 	s[1].sub_topic.msg_len = 3;
   82: 	s[1].sub_topic.msg_base = "c/d";
   83: 	s[1].sub_ret = MQTT_QOS_ONCE;
   84: 	s[2].sub_topic.msg_len = 7;
   85: 	s[2].sub_topic.msg_base = "x/y/z/Q";
   86: 	s[2].sub_ret = MQTT_QOS_EXACTLY;
   87: 	printf("subscribe=%d/%d\n", m->msg_len, mqtt_msgSUBSCRIBE(m, s, 10, 0, 0));
   88: 	for (i = 0; i < m->msg_len; i++)
   89: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
   90: 	if (mqtt_readSUBSCRIBE(m, &msgID, &ss) == -1) {
   91: 		printf("Error:: mqtt_readSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   92: 		return 3;
   93: 	}
   94: 	printf("read subscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
   95: 	printf("++> MessageID=%d\n", msgID);
   96: 	for (i = 0; ss[i].sub_topic.msg_base; i++)
   97: 		printf(" >>> QoS=%d Topic(%d)=%s\n", ss[i].sub_ret, ss[i].sub_topic.msg_len, 
   98: 				(char*) ss[i].sub_topic.msg_base);
   99: 	mqtt_subFree(&ss);
  100: 	printf("suback=%d/%d\n", m->msg_len, mqtt_msgSUBACK(m, s, 10));
  101: 	for (i = 0; i < m->msg_len; i++)
  102: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
  103: 	printf("read suback=%d\n", (len = mqtt_readSUBACK(m, &msgID, &qoses)));
  104: 	for (i = 0; i < len; i++)
  105: 		printf(" >>> QoS=%d\n", qoses[i]);
  106: 	free(qoses);
  107: 	printf("unsubscribe=%d/%d\n", m->msg_len, mqtt_msgUNSUBSCRIBE(m, s, 10, 0, 1));
  108: 	for (i = 0; i < m->msg_len; i++)
  109: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
  110: 	if (mqtt_readUNSUBSCRIBE(m, &msgID, &ss) == -1) {
  111: 		printf("Error:: mqtt_readUNSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
  112: 		return 3;
  113: 	}
  114: 	printf("read unsubscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
  115: 	printf("++> MessageID=%d\n", msgID);
  116: 	for (i = 0; ss[i].sub_topic.msg_base; i++)
  117: 		printf(" >>> Topic(%d)=%s\n", ss[i].sub_topic.msg_len, (char*) ss[i].sub_topic.msg_base);
  118: 	mqtt_subFree(&ss);
  119: 	printf("unsuback=%d/%d\n", m->msg_len, mqtt_msgUNSUBACK(m, 10));
  120: 	for (i = 0; i < m->msg_len; i++)
  121: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
  122: 	printf("read unsuback=%d\n", mqtt_readUNSUBACK(m));
  123: 	getchar();
  124: 
  125: 	/* ping* */
  126: 	printf("pingreq=%d/%d\n", m->msg_len, mqtt_msgPINGREQ(m));
  127: 	for (i = 0; i < m->msg_len; i++)
  128: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
  129: 	printf("read pingreq=%d\n", mqtt_readPINGREQ(m));
  130: 	printf("pingresp=%d/%d\n", m->msg_len, mqtt_msgPINGRESP(m));
  131: 	for (i = 0; i < m->msg_len; i++)
  132: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
  133: 	printf("read pingresp=%d\n", mqtt_readPINGRESP(m));
  134: 
  135: 	printf("disconnect=%d/%d\n", m->msg_len, mqtt_msgDISCONNECT(m));
  136: 	for (i = 0; i < m->msg_len; i++)
  137: 		printf("%d\n", ((u_char*) m->msg_base)[i]);
  138: 	printf("read disconnect=%d\n", mqtt_readDISCONNECT(m));
  139: 
  140: 	mqtt_msgFree(&m, 42);
  141: 	return 0;
  142: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>