Пример #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;

    // 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;
}