Annotation of mqtt/src/conn.c, revision 1.1.2.3
1.1.2.1 misho 1: #include "global.h"
2:
3:
4: /* ------------------------------------------------------------------- */
5:
6: /*
7: * mqtt_msgCONNECT() Create CONNECT message
8: *
9: * @buf = Message buffer
10: * @csConnID = ConnectID
11: * @csUser = Username if !=NULL
12: * @csPass = Password for Username, only if csUser is set
13: * @csWillTopic = Will Topic if !=NULL Will Flags set into message
14: * @csWillMessage = Will Message, may be NULL
15: * @ClrSess = Clear Session subscriptions after disconnect
16: * @WillQOS = Will QOS if csWillTopic is set
17: * @WillRetain = Will Retain Will Message if csWillTopic is set
18: * return: -1 error or >-1 message size for send
19: */
20: int
21: mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID,
22: const char *csUser, const char *csPass,
23: const char *csWillTopic, const char *csWillMessage,
1.1.2.3 ! misho 24: u_char ClrSess, u_char WillQOS, u_char WillRetain)
1.1.2.1 misho 25: {
26: int siz = 0;
27: struct mqtthdr *hdr;
28: mqtthdr_var_t *var, *cid, *topic, *wmsg, *user, *pass;
29: mqtthdr_protover_t *proto;
30: mqtthdr_connflgs_t *flags;
31: mqtt_v_t *ka;
32:
33: if (!buf || !csConnID)
34: return -1;
35: if (strlen(csConnID) > 23) {
36: mqtt_SetErr(EINVAL, "Error:: invalid argument ConnID is too long (max 23 bytes)");
37: return -1;
38: }
39: if (csUser && strlen(csUser) > 12) {
40: mqtt_SetErr(EINVAL, "Error:: invalid argument Username is too long (max 12 bytes)");
41: return -1;
42: }
43: if (csPass && strlen(csPass) > 12) {
44: mqtt_SetErr(EINVAL, "Error:: invalid argument Password is too long (max 12 bytes)");
45: return -1;
46: }
1.1.2.3 ! misho 47: if (WillQOS > MQTT_QOS_EXACTLY) {
1.1.2.1 misho 48: mqtt_SetErr(EINVAL, "Error:: invalid argument WillQOS - unknown QOS value");
49: return -1;
50: }
51:
52: if (mqtt_msgRealloc(buf, BUFSIZ) == -1)
53: return -1;
54: else {
55: hdr = (struct mqtthdr *) (buf->msg_base + siz);
1.1.2.2 misho 56: siz += sizeof(struct mqtthdr);
1.1.2.1 misho 57: var = (mqtthdr_var_t*) (buf->msg_base + siz);
58: siz += 8;
59: proto = buf->msg_base + siz;
60: siz++;
61: flags = (mqtthdr_connflgs_t*) (buf->msg_base + siz);
62: siz++;
63: ka = (mqtt_v_t*) (buf->msg_base + siz);
64: siz += sizeof(mqtt_v_t);
65: }
66:
67: /* fixed header */
68: hdr->mqtt_msg.type = MQTT_TYPE_CONNECT;
69: *hdr->mqtt_len = 0;
70:
71: /* variable header */
72: var->var_sb.sb.l = 6;
73: memcpy(var->var_data, MQTT_CONN_STR, 6);
74:
75: *proto = MQTT_PROTO_VER;
76:
77: /* CONNECT header */
78: flags->clean_sess = ClrSess ? 1 : 0;
79: if (csUser) {
80: flags->username = 1;
81: flags->password = csPass ? 1 : 0;
82: } else {
83: flags->username = 0;
84: flags->password = 0;
85: }
86: if (csWillTopic) {
87: flags->will_flg = 1;
88: flags->will_qos = WillQOS;
89: flags->will_retain = WillRetain ? 1 : 0;
90: } else {
91: flags->will_flg = 0;
92: flags->will_qos = 0;
93: flags->will_retain = 0;
94: }
95:
96: ka->sb.l = MQTT_KEEPALIVE;
97:
98: /* ConnID */
99: cid = (mqtthdr_var_t*) (buf->msg_base + siz);
100: cid->var_sb.val = htons(strlen(csConnID));
101: siz += MQTTHDR_VAR_SIZEOF(cid);
102: memcpy(cid->var_data, csConnID, ntohs(cid->var_sb.val));
103:
104: /* If Will Flags setup */
105: if (csWillTopic) {
106: topic = (mqtthdr_var_t*) (buf->msg_base + siz);
107: topic->var_sb.val = htons(strlen(csWillTopic));
108: memcpy(topic->var_data, csWillTopic, ntohs(topic->var_sb.val));
109: siz += MQTTHDR_VAR_SIZEOF(topic);
110:
111: wmsg = (mqtthdr_var_t*) (buf->msg_base + siz);
112: if (csWillMessage) {
113: wmsg->var_sb.val = htons(strlen(csWillMessage));
114: memcpy(wmsg->var_data, csWillMessage, ntohs(wmsg->var_sb.val));
115: } else
116: wmsg->var_sb.val = 0;
117: siz += MQTTHDR_VAR_SIZEOF(wmsg);
118: }
119:
120: /* If defined Username & Password */
121: if (csUser) {
122: user = (mqtthdr_var_t*) (buf->msg_base + siz);
123: user->var_sb.val = htons(strlen(csUser));
124: memcpy(user->var_data, csUser, ntohs(user->var_sb.val));
125: siz += MQTTHDR_VAR_SIZEOF(user);
126:
127: if (csPass) {
128: pass = (mqtthdr_var_t*) (buf->msg_base + siz);
129: pass->var_sb.val = htons(strlen(csPass));
130: memcpy(pass->var_data, csPass, ntohs(pass->var_sb.val));
131: siz += MQTTHDR_VAR_SIZEOF(pass);
132: }
133: }
134:
1.1.2.3 ! misho 135: *hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr));
1.1.2.1 misho 136: mqtt_msgRealloc(buf, siz);
137: return siz;
138: }
1.1.2.2 misho 139:
140: /*
141: * mqtt_msgCONNACK() Create CONNACK message
142: *
143: * @buf = Message buffer
144: * @retcode = Return code
145: * return: -1 error or >-1 message size for send
146: */
147: int
148: mqtt_msgCONNACK(mqtt_msg_t * __restrict buf, u_char retcode)
149: {
150: int siz = 0;
151: struct mqtthdr *hdr;
152: mqtthdr_connack_t *ack;
153:
154: if (!buf)
155: return -1;
156: if (retcode > MQTT_RETCODE_DENIED) {
157: mqtt_SetErr(EINVAL, "Error:: invalid retcode");
158: return -1;
159: }
160:
161: if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtthdr_connack_t)) == -1)
162: return -1;
163: else {
164: hdr = (struct mqtthdr *) (buf->msg_base + siz);
165: siz += sizeof(struct mqtthdr);
166: ack = (mqtthdr_connack_t*) (buf->msg_base + siz);
167: siz += sizeof(mqtthdr_connack_t);
168: }
169:
170: /* fixed header */
171: hdr->mqtt_msg.type = MQTT_TYPE_CONNACK;
172: *hdr->mqtt_len = sizeof(mqtthdr_connack_t);
173:
174: /* CONNACK header */
175: ack->reserved = 0;
176: ack->retcode = retcode;
177:
178: return siz;
179: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>