Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
qos0pub_register.c
00001 /******************************************************************************* 00002 * Copyright (c) 2014 IBM Corp. 00003 * 00004 * All rights reserved. This program and the accompanying materials 00005 * are made available under the terms of the Eclipse Public License v1.0 00006 * and Eclipse Distribution License v1.0 which accompany this distribution. 00007 * 00008 * The Eclipse Public License is available at 00009 * http://www.eclipse.org/legal/epl-v10.html 00010 * and the Eclipse Distribution License is available at 00011 * http://www.eclipse.org/org/documents/edl-v10.php. 00012 * 00013 * Contributors: 00014 * Ian Craggs - initial API and implementation and/or initial documentation 00015 * Sergio R. Caprile - clarifications and/or documentation extension 00016 * 00017 * Description: 00018 * Normal topic name used to show registration process 00019 *******************************************************************************/ 00020 00021 #include <stdio.h> 00022 #include <string.h> 00023 #include <stdlib.h> 00024 00025 #include "MQTTSNPacket.h" 00026 #include "lowlevel.h" 00027 00028 00029 int main(int argc, char** argv) 00030 { 00031 int rc = 0; 00032 int mysock; 00033 unsigned char buf[200]; 00034 int buflen = sizeof(buf); 00035 MQTTSN_topicid topic; 00036 MQTTSNString topicstr; 00037 unsigned char* payload = (unsigned char*)"mypayload"; 00038 int payloadlen = strlen((char*)payload); 00039 int len = 0; 00040 int dup = 0; 00041 int qos = 0; 00042 int retained = 0; 00043 short packetid = 0; 00044 char *topicname = "a long topic name"; 00045 char *host = "127.0.0.1"; 00046 int port = 1883; 00047 MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer; 00048 unsigned short topicid; 00049 00050 mysock = lowlevel_open(); 00051 if(mysock < 0) 00052 return mysock; 00053 00054 if (argc > 1) 00055 host = argv[1]; 00056 00057 if (argc > 2) 00058 port = atoi(argv[2]); 00059 00060 printf("Sending to hostname %s port %d\n", host, port); 00061 00062 options.clientID.cstring = "myclientid"; 00063 len = MQTTSNSerialize_connect(buf, buflen, &options); 00064 rc = lowlevel_sendPacketBuffer(host, port, buf, len); 00065 00066 /* wait for connack */ 00067 if (MQTTSNPacket_read(buf, buflen, lowlevel_getdata) == MQTTSN_CONNACK) 00068 { 00069 int connack_rc = -1; 00070 00071 if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0) 00072 { 00073 printf("Unable to connect, return code %d\n", connack_rc); 00074 goto exit; 00075 } 00076 else 00077 printf("connected rc %d\n", connack_rc); 00078 } 00079 else 00080 goto exit; 00081 00082 /* register topic name */ 00083 printf("Registering\n"); 00084 topicstr.cstring = topicname; 00085 topicstr.lenstring.len = strlen(topicname); 00086 len = MQTTSNSerialize_register(buf, buflen, 0, packetid, &topicstr); 00087 rc = lowlevel_sendPacketBuffer(host, port, buf, len); 00088 00089 if (MQTTSNPacket_read(buf, buflen, lowlevel_getdata) == MQTTSN_REGACK) /* wait for regack */ 00090 { 00091 unsigned short submsgid; 00092 unsigned char returncode; 00093 00094 rc = MQTTSNDeserialize_regack(&topicid, &submsgid, &returncode, buf, buflen); 00095 if (returncode != 0) 00096 { 00097 printf("return code %d\n", returncode); 00098 goto exit; 00099 } 00100 else 00101 printf("regack topic id %d\n", topicid); 00102 } 00103 else 00104 goto exit; 00105 00106 /* publish with obtained id */ 00107 printf("Publishing\n"); 00108 topic.type = MQTTSN_TOPIC_TYPE_NORMAL; 00109 topic.data.id = topicid; 00110 ++packetid; 00111 len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid, 00112 topic, payload, payloadlen); 00113 rc = lowlevel_sendPacketBuffer(host, port, buf, len); 00114 00115 printf("rc %d from send packet for publish length %d\n", rc, len); 00116 00117 exit: 00118 lowlevel_close(); 00119 00120 return 0; 00121 }
Generated on Wed Jul 13 2022 10:46:03 by
1.7.2