File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / example / Attic / cmds.c
Revision 1.1.2.14: download - view: text, annotated - select for diffs - revision graph
Tue Dec 6 09:04:55 2011 UTC (12 years, 8 months ago) by misho
Branches: mqtt1_0
finish publish read apis

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

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