Annotation of mqtt/src/pub.c, revision 1.1.2.5
1.1.2.1 misho 1: #include "global.h"
2:
3:
4: /* ------------------------------------------------------------------- */
5:
1.1.2.2 misho 6: /*
7: * mqtt_msgPUBLISH() Create PUBLISH message
8: *
9: * @buf = Message buffer
10: * @csTopic = Publish topic
11: * @msgID = MessageID >0, if QOS != MQTT_QOS_ONCE
12: * @Dup = Duplicate message
13: * @QOS = QoS
1.1.2.4 misho 14: * @Retain = Retain message
1.1.2.5 ! misho 15: * @pData = Publish data into topic
! 16: * @datlen = Publish data length
1.1.2.2 misho 17: * return: -1 error or >-1 message size for send
18: */
19: int
20: mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const char *csTopic, u_short msgID,
1.1.2.5 ! misho 21: u_char Dup, u_char QOS, u_char Retain, const void *pData, u_short datlen)
1.1.2.2 misho 22: {
23: int siz = 0;
24: struct mqtthdr *hdr;
25: mqtthdr_var_t *topic;
26: mqtt_v_t *mid;
1.1.2.5 ! misho 27: void *data;
1.1.2.2 misho 28:
29: if (!buf || !csTopic)
30: return -1;
31: if (QOS > MQTT_QOS_EXACTLY) {
32: mqtt_SetErr(EINVAL, "Error:: invalid QoS parameter");
33: return -1;
34: }
35: if (!msgID && QOS != MQTT_QOS_ONCE) {
36: mqtt_SetErr(EINVAL, "Error:: invalid MessageID parameter must be >0");
37: return -1;
38: }
39:
40: if (mqtt_msgRealloc(buf, MQTTMSG_MAX) == -1)
41: return -1;
42: else {
43: hdr = (struct mqtthdr *) (buf->msg_base + siz);
44: siz += sizeof(struct mqtthdr);
45: }
46:
47: /* variable header */
48:
49: topic = (mqtthdr_var_t*) (buf->msg_base + siz);
50: topic->var_sb.val = htons(strlen(csTopic));
51: memcpy(topic->var_data, csTopic, ntohs(topic->var_sb.val));
52: siz += MQTTHDR_VAR_SIZEOF(topic);
53:
54: mid = (mqtt_v_t*) (buf->msg_base + siz);
55: mid->val = htons(msgID);
56: siz += sizeof(mqtt_v_t);
57:
1.1.2.5 ! misho 58: /* load with data */
! 59:
! 60: if (pData && datlen) {
! 61: data = buf->msg_base + siz;
! 62: memcpy(data, pData, datlen);
! 63: siz += datlen;
! 64: }
! 65:
1.1.2.2 misho 66: /* fixed header */
67:
68: hdr->mqtt_msg.type = MQTT_TYPE_PUBLISH;
69: hdr->mqtt_msg.qos = QOS;
70: hdr->mqtt_msg.dup = Dup ? 1 : 0;
1.1.2.4 misho 71: hdr->mqtt_msg.retain = Retain ? 1 : 0;
1.1.2.2 misho 72: *hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr));
73:
74: mqtt_msgRealloc(buf, siz);
75: return siz;
76: }
77:
78: static int
79: _mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, u_short msgID)
80: {
81: int siz = 0;
82: struct mqtthdr *hdr;
83: mqtt_v_t *v;
84:
85: if (!buf)
86: return -1;
87:
88: if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtt_v_t)) == -1)
89: return -1;
90: else {
91: hdr = (struct mqtthdr *) (buf->msg_base + siz);
92: siz += sizeof(struct mqtthdr);
93: v = (mqtt_v_t*) (buf->msg_base + siz);
94: siz += sizeof(mqtt_v_t);
95: }
96:
97: /* fixed header */
98: hdr->mqtt_msg.type = cmd;
99: *hdr->mqtt_len = sizeof(mqtt_v_t);
100:
101: /* MessageID */
102: v->val = htons(msgID);
103:
104: return siz;
105: }
106:
107: /*
108: * mqtt_msgPUBACK() Create PUBACK message
109: *
110: * @buf = Message buffer
111: * @msgID = MessageID
112: * return: -1 error or >-1 message size for send
113: */
114: inline int
115: mqtt_msgPUBACK(mqtt_msg_t * __restrict buf, u_short msgID)
116: {
117: return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBACK, msgID);
118: }
119:
120: /*
121: * mqtt_msgPUBREC() Create PUBREC message
122: *
123: * @buf = Message buffer
124: * @msgID = MessageID
125: * return: -1 error or >-1 message size for send
126: */
127: inline int
128: mqtt_msgPUBREC(mqtt_msg_t * __restrict buf, u_short msgID)
129: {
130: return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBREC, msgID);
131: }
132:
133: /*
134: * mqtt_msgPUBREL() Create PUBREL message
135: *
136: * @buf = Message buffer
137: * @msgID = MessageID
138: * return: -1 error or >-1 message size for send
139: */
140: inline int
141: mqtt_msgPUBREL(mqtt_msg_t * __restrict buf, u_short msgID)
142: {
143: return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBREL, msgID);
144: }
145:
146: /*
147: * mqtt_msgPUBCOMP() Create PUBCOMP message
148: *
149: * @buf = Message buffer
150: * @msgID = MessageID
151: * return: -1 error or >-1 message size for send
152: */
153: inline int
154: mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, u_short msgID)
155: {
156: return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBCOMP, msgID);
157: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>