Пример #1
0
int main(int argc, char* argv[])
{
    int sock;

    // Parse the command-line options
    parse_opts(argc, argv);

    // Enable debugging?
    mqtt_sn_set_debug(debug);
    // Create a UDP socket
    sock = mqtt_sn_create_socket(mqtt_sn_host, mqtt_sn_port);
    if (sock) {
        // Connect to gateway
        if (qos >= 0) {
           if (qos==0) {
                mqtt_sn_send_connect(sock, client_id, MQTT_SN_FLAG_CLEAN, keep_alive);
                mqtt_sn_receive_connack(sock);
            } else if (qos==1) {
                mqtt_sn_send_connect(sock, client_id, MQTT_SN_FLAG_CLEAN | MQTT_SN_FLAG_WILL , keep_alive);
                connack_packet_t *p= mqtt_sn_receive_packet(sock);
                if ( p!= NULL && p->type==MQTT_SN_TYPE_WILLTOPICREQ) {
                    mqtt_sn_send_will_topic(sock, t_topic_name, MQTT_SN_FLAG_QOS_1 | MQTT_SN_FLAG_RETAIN);
                    p= mqtt_sn_receive_packet(sock);
                    if ( p!= NULL && p->type==MQTT_SN_TYPE_WILLMSGREQ) {
                        mqtt_sn_send_will_msg(sock, t_message_data);
                    } else {
                        fprintf(stderr,"Error: did not get MQTT_SN_TYPE_WILLMSGREQ.\n");
                        exit(-1);
                    }
                } else {
                    fprintf(stderr,"Error: did not get MQTT_SN_TYPE_WILLTOPICREQ.\n");
                    exit(-1);
                }
                mqtt_sn_receive_connack(sock);
            }
        }
        if (topic_id) {
            // Use pre-defined topic ID
            topic_id_type = MQTT_SN_TOPIC_TYPE_PREDEFINED;
        } else if (strlen(topic_name) == 2) {
            // Convert the 2 character topic name into a 2 byte topic id
            topic_id = (topic_name[0] << 8) + topic_name[1];
            topic_id_type = MQTT_SN_TOPIC_TYPE_SHORT;
        } else if (qos >= 0) {
             // Register the topic name
             mqtt_sn_send_register(sock, topic_name);
             topic_id = mqtt_sn_receive_regack(sock);
             topic_id_type = MQTT_SN_TOPIC_TYPE_NORMAL;
        }

        // Publish to the topic
        mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
        if (qos==1) {
            int retries=0;
            while (1) {
                connack_packet_t *p= mqtt_sn_receive_packet(sock);
                if ( p!= NULL && p->type==MQTT_SN_TYPE_PUBACK) {
                    break;
                } else 
                    fprintf(stderr,"Warn: QoS 1 and send not acked -- retrying.. (%d/10)\n",retries);
                if (retries++>10) {
                    fprintf(stderr,"Error: QoS 1 and send not acked, tried %d times\n",retries);
                    exit(-1);
                }
                sleep(1);
                mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
            }
            if (retries)
                fprintf(stderr,"Warn: Send required %d times, but was successful.\n",retries);

        }
        // Finally, disconnect
        if (qos >= 0) {
            mqtt_sn_send_disconnect(sock);
            connack_packet_t *p= mqtt_sn_receive_packet(sock);
            if ( p!= NULL && p->type==MQTT_SN_TYPE_DISCONNECT) {
                // disconnect acked ok
            } else 
                fprintf(stderr,"Warn: QoS >=0 and DISCONNECT not acked\n");
        }
        close(sock);
    }

    mqtt_sn_cleanup();

    return 0;
}
Пример #2
0
int main(int argc, char* argv[])
{
	int sock;
	
	/* Define a pointer to an item in the tail queue. */
	struct tailq_entry *item;

        /* In some cases we have to track a temporary item. */
        struct tailq_entry *tmp_item;

	/* Initialize the tail queue. */
	TAILQ_INIT(&my_tailq_head);

	//Create UDP packet
	sock = mqtt_sn_create_socket(mqtt_sn_host, mqtt_sn_port);

	if (sock)
	{
		// Connect to gateway
	        if (qos >= 0) 
		{
            		mqtt_sn_send_connect(sock, client_id, keep_alive);
            		mqtt_sn_recieve_connack(sock);
        	}

		if (topic_id) 
		{
		    // Use pre-defined topic ID
		    topic_id_type = MQTT_SN_TOPIC_TYPE_PREDEFINED;
		} 
		else if (strlen(topic_name) == 2) 
		{
		    // Convert the 2 character topic name into a 2 byte topic id
		    topic_id = (topic_name[0] << 8) + topic_name[1];
		    topic_id_type = MQTT_SN_TOPIC_TYPE_SHORT;
		} 
		else if (qos >= 0) 
		{
		    // Register the topic name
		    mqtt_sn_send_register(sock, topic_name);
		    topic_id = mqtt_sn_recieve_regack(sock);
		    topic_id_type = MQTT_SN_TOPIC_TYPE_NORMAL;
		}
		
		int count = 1;
		char strbuf[255];
		int sent = 1;

		while (TRUE)
		{
			if (sent == 0)
			{
				sent = 	mqtt_sn_send_publish(sock, topic_id, topic_id_type, "test", qos, FALSE);
				if (sent == 0)
				{
					sprintf(strbuf, "%04d", count);
					count++;
					//add to the queue
					item = malloc(sizeof(*item));
					if (item == NULL) {
						perror("malloc failed");
						exit(EXIT_FAILURE);
					}

					item->value = strdup(strbuf);
					TAILQ_INSERT_TAIL(&my_tailq_head, item, entries);

					printf("No connection, message %s stored.\n", item->value);
					
				}
				else if (sent == 1)
				{
					//tmp_item = malloc(sizeof(*tmp_item));

					while(sent == 1 && !TAILQ_EMPTY(&my_tailq_head))
					{
						tmp_item = malloc(sizeof(*tmp_item));
						tmp_item = TAILQ_FIRST(&my_tailq_head);
						sent = mqtt_sn_send_publish(sock, topic_id, topic_id_type, tmp_item->value, qos, retain);
						
						if (sent == 1)
						{
							//If message was sent remove the message
							fprintf(stderr, "Stored Message %s Successfully Sent\n", tmp_item->value);
							TAILQ_REMOVE(&my_tailq_head, tmp_item, entries);
							free(tmp_item);
						}
					}	
				}

			}
			else
			{
				sprintf(strbuf, "%04d", count);
				count++;
				// Publish to the topic
				sent = mqtt_sn_send_publish(sock, topic_id, topic_id_type, strbuf, qos, retain);
				if (sent == 1)
				{
					fprintf(stderr, "Published we did\n");
				}
				else if (sent == 0)
				{
					//add to the queue
					item = malloc(sizeof(*item));
					if (item == NULL) {
						perror("malloc failed");
						exit(EXIT_FAILURE);
					}
					item->value = strdup(strbuf);
					TAILQ_INSERT_TAIL(&my_tailq_head, item, entries);

					fprintf(stderr, "No connection, message %s stored.\n", item->value);
				}
			}
			sleep(5);
		}

	}
	
	return 0;
	
}
Пример #3
0
int main(int argc, char* argv[])
{
    int sock;

    // Parse the command-line options
    parse_opts(argc, argv);
    
    int i=1;

    //Variables for time taking
    struct timeval start, end;
    // FILE *fp;


    // Create a UDP socket
    sock = mqtt_sn_create_socket(mqtt_sn_host, mqtt_sn_port);
    
    if (sock) {
        // Connect to gateway
        if (qos >= 0) {
            mqtt_sn_send_connect(sock, client_id, keep_alive);
            mqtt_sn_receive_connack(sock);
        }

        if (topic_id) {
            // Use pre-defined topic ID
            topic_id_type = MQTT_SN_TOPIC_TYPE_PREDEFINED;
        } else if (strlen(topic_name) == 2) {
            // Convert the 2 character topic name into a 2 byte topic id
            topic_id = (topic_name[0] << 8) + topic_name[1];
            topic_id_type = MQTT_SN_TOPIC_TYPE_SHORT;
        } else if (qos >= 0) {
            // Register the topic name
            mqtt_sn_send_register(sock, topic_name);
            topic_id = mqtt_sn_receive_regack(sock);
            topic_id_type = MQTT_SN_TOPIC_TYPE_NORMAL;
        }

        // gettimeofday(&start, NULL);
        while(i<=1000)
        {
         gettimeofday(&start, NULL);
        // Publish to the topic 
        if (qos == 0){
        mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
        }

        
        // Manage Return Packets and Retrasnmits - harith
        if (qos >= 1){
            
            mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);

            if (qos == 1){

                struct timeval tv;
                fd_set rfd;
                int ret;
                int resend = 0;

                while (resend < 5){

                FD_ZERO(&rfd);
                FD_SET(sock, &rfd);

                tv.tv_sec = 1;
                tv.tv_usec = 0;

                ret = select(FD_SETSIZE, &rfd, NULL, NULL, &tv);
                    if (ret < 0) {
                        printf("Select() Error!\n" );
                        exit(EXIT_FAILURE);
                    }
                    else if (ret > 0) {
                        // Receive a packet
                        mqtt_sn_receive_puback(sock);
                        resend = 6;
                    }
                    else if (ret == 0)
                    {
                        printf("republishing...\n");
                        mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
                        resend++;
                    }
                }
            }

            //QOS=2
            else if(qos==2){

                struct timeval tv;
                fd_set rfd;
                int ret;
                int resendpublish = 0;
                int resendpubrel = 0;

                while (resendpublish < 5){

                FD_ZERO(&rfd);
                FD_SET(sock, &rfd);

                tv.tv_sec = 1;
                tv.tv_usec = 0;

                ret = select(FD_SETSIZE, &rfd, NULL, NULL, &tv);
                    if (ret < 0) {
                        printf("Select() Error!\n" );
                        exit(EXIT_FAILURE);
                    }
                    else if (ret > 0) {

                        // Receive a packet
                        mqtt_sn_receive_pubrec(sock);
                        resendpublish = 6;
                        mqtt_sn_send_pubrel(sock);
                    }
                    else if (ret == 0)
                    {
                        printf("republishing qos21...\n");
                        mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
                        resendpublish++;
                    }
                }

                //retransmit pubrel
                while (resendpubrel < 5){

                FD_ZERO(&rfd);
                FD_SET(sock, &rfd);

                tv.tv_sec = 1;
                tv.tv_usec = 0;

                ret = select(FD_SETSIZE, &rfd, NULL, NULL, &tv);
                    if (ret < 0) {
                        printf("Select() Error!\n" );
                        exit(EXIT_FAILURE);
                    }
                    else if (ret > 0) {

                        // Receive a packet
                        mqtt_sn_receive_pubcomp(sock);
                        resendpubrel = 6;
                    }
                    else if (ret == 0)
                    {
                        printf("republishing qos22...\n");
                        mqtt_sn_send_pubrel(sock);
                        resendpubrel++;
                    }
                } 

            }
        }
        
        gettimeofday(&end, NULL);

        // int taken = (int)((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec))/1000000;
        float taken = (float)((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec))/1000;

        printf("Time taken is:%3.2f ms\n\n",taken);
       
        //open file for writing
        // fp = fopen( "logfile.csv", "a" ); // Open file for writing
        // fprintf(fp, "Seq %d , %3.2fms, ",i,taken);
        // fprintf(fp,"Time %s\r\n",ctime((const time_t *) &end.tv_sec));
        // fclose(fp);

        // printf("Time taken is:%3.2d s\n\n", (int)((end.tv_sec * 1000000 + end.tv_usec)
        //   - (start.tv_sec * 1000000 + start.tv_usec))/1000000);
        
        sleep(.5);
        i++;
        // if(taken>=2){
        //     printf("%d messages sent.\n", i);    
        //     exit(EXIT_SUCCESS);
        // }
        
        }
        
        if (qos >= 0) {
            mqtt_sn_send_disconnect(sock);
            mqtt_sn_receive_disconnect(sock);
        }

        close(sock);
    }
    
    mqtt_sn_cleanup();

  

    return 0;
}