aws / aws-iot-device-sdk-embedded-C

SDK for connecting to AWS IoT from a device using embedded C.
MIT License
980 stars 632 forks source link

Failed to send data over network: SSL_write of OpenSSL failed #1740

Closed una801 closed 2 years ago

una801 commented 3 years ago

Hello I succeeded in a project to send CAN Data to the server using AWS IoT Embedded C SDK.

This time, the following error occurs during the test to send the received data to the server after writing RS232 data of the battery pack.

[INFO] [DEMO] [mqtt_demo_mutual_auth.c:989] Subscribing to the MQTT topic PESS/IoT/Battery. [ERROR] [Transport_OpenSSL_Sockets] [openssl_posix.c:814] Failed to send data over network: SSL_write of OpenSSL failed: ErrorStatus=(null). [ERROR] [MQTT] [core_mqtt.c:605] Transport send failed. Error code=-1. [ERROR] [Transport_OpenSSL_Sockets] [openssl_posix.c:761] Failed to receive data over network: SSL_read failed: ErrorStatus=(null). [ERROR] [MQTT] [core_mqtt_serializer.c:2396] A single byte was not read from the transport: transportStatus=-1. [ERROR] [MQTT] [core_mqtt.c:1300] Receiving incoming packet length failed. Status=MQTTRecvFailed [ERROR] [MQTT] [core_mqtt.c:2172] Exiting process loop due to failure: ErrorStatus=MQTTRecvFailed [ERROR] [DEMO] [mqtt_demo_mutual_auth.c:1032] MQTT_ProcessLoop returned with status = MQTTRecvFailed. [INFO] [MQTT] [core_mqtt.c:2129] Disconnected from the broker.

Since the previous CAN Data transmission was successful, it seems that there was an abnormality while changing the part that publicizes RS232 Data, but I don't know what the problem is. (There is no modification except for the Public part.)

The following is the Public Code that I modified.

static int publishToTopic( MQTTContext_t * pMqttContext )
{
    int returnStatus = EXIT_SUCCESS;
    MQTTStatus_t mqttStatus = MQTTSuccess;
    uint8_t publishIndex = MAX_OUTGOING_PUBLISHES;
    uint32_t publishCount = 0;
    const uint32_t maxPublishCount = MQTT_PUBLISH_COUNT_PER_LOOP;   
    char message[1024]={0x00, };
    int fd;
    fd=open("/dev/ttyHSL6", O_RDWR|O_NOCTTY );
    struct termios newtio;  
    unsigned char buffer[50]={0,};
    int bytes_read=0;
    char data[200];
    char *data3;
    char data4[50];
    char *batteryData;
    int i=0;
    int j;
    char a[]="a5";
    unsigned char str[13] = {0xA5,0x80,0x95,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC2};
    unsigned char str1[13] = {0xA5,0x80,0x90,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBD};
    unsigned char str2[13] = {0xA5,0x80,0x91,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBE};
    unsigned char str3[13] = {0xA5,0x80,0x92,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBF};
    unsigned char str4[13] = {0xA5,0x80,0x93,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0};
    unsigned char str5[13] = {0xA5,0x80,0x94,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC1};
    unsigned char str6[13] = {0xA5,0x80,0x96,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC3};
    unsigned char str7[13] = {0xA5,0x80,0x98,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC5};

    if (fd == -1)
    {
        printf("Error! in opening port");
        exit(-100);
    }

    memset(&newtio, 0, sizeof(struct termios));
    newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;

    newtio.c_iflag = IGNPAR;

    newtio.c_oflag = 0;
    newtio.c_lflag = 0;
    newtio.c_cc[VTIME] = 0; 
    newtio.c_cc[VMIN] = 1; 
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &newtio);

        data3=malloc(sizeof(char)*50);

        batteryData=malloc(sizeof(char)*50);
        memset(buffer, 0, sizeof(buffer));

        write(fd, str, sizeof(str)/sizeof(str[0]));
        usleep(10000);
        write(fd, str1, sizeof(str1)/sizeof(str1[0]));
        usleep(1000);
        write(fd, str2, sizeof(str2)/sizeof(str2[0]));
        usleep(1000);
        write(fd, str3, sizeof(str3)/sizeof(str3[0]));
        usleep(1000);
        write(fd, str4, sizeof(str4)/sizeof(str4[0]));
        usleep(1000);
        write(fd, str5, sizeof(str5)/sizeof(str5[0]));
        usleep(1000);
        write(fd, str6, sizeof(str6)/sizeof(str6[0]));
        usleep(1000);       
        write(fd, str7, sizeof(str7)/sizeof(str7[0]));
        usleep(1000);

        read(fd, buffer, sizeof(buffer)/sizeof(buffer[0]));

        if(buffer[0]==0xA5){        

            sprintf(data,"%02x",buffer[0]);
            strcpy(data3,data);

            for (j=1; j<15; j++)        
            {
                sprintf(data,"%02x",buffer[j]);
                strcat(data3," ");
                strcat(data3,data);
            }

            strcpy(data4,data3);

        }   

    sprintf(batteryData,"{\"Battery\":\"%s\"}",data4);

    if( returnStatus == EXIT_FAILURE )
    {
        LogError( ( "Unable to find a free spot for outgoing PUBLISH message.\n\n" ) );
    }
    else
    {

        outgoingPublishPackets[ publishIndex ].pubInfo.qos = MQTTQoS1;
        outgoingPublishPackets[ publishIndex ].pubInfo.pTopicName = MQTT_EXAMPLE_TOPIC;
        outgoingPublishPackets[ publishIndex ].pubInfo.topicNameLength = MQTT_EXAMPLE_TOPIC_LENGTH;
        outgoingPublishPackets[ publishIndex ].pubInfo.pPayload = batteryData;
        outgoingPublishPackets[ publishIndex ].pubInfo.payloadLength =strlen(batteryData);
        outgoingPublishPackets[ publishIndex ].packetId = MQTT_GetPacketId( pMqttContext );

        mqttStatus = MQTT_Publish( pMqttContext,
                &outgoingPublishPackets[ publishIndex ].pubInfo,
                outgoingPublishPackets[ publishIndex ].packetId );

        if( mqttStatus != MQTTSuccess )
        {
            LogError( ( "Failed to send PUBLISH packet to broker with error = %s.",
                        MQTT_Status_strerror( mqttStatus ) ) );
            cleanupOutgoingPublishAt( publishIndex );
            returnStatus = EXIT_FAILURE;
        }
        else
        {

        }
    }   
     //( void ) shutdown( fd, SHUT_RDWR );
     close( fd );
    return returnStatus;
}

Before sending it to the server, I used a simple printf to confirm that the data was read and then applied the code inside the Public function.

Can I know what the problem is? Please help me.

yanjos-dev commented 3 years ago

Hello @una801, what version of the repository you are using?

una801 commented 3 years ago

Sorry for the late reply. The confirmation was delayed because it was the weekend. I don't know how to check the repository version....

In my opinion, there is no problem with receiving CAN Data, but I thought it was a problem with the codeword because I saw only RS232 Data as having a problem, but is it a problem with the version apart from that code?

una801 commented 3 years ago

please help me..

tianmc1 commented 2 years ago

Hi @una801 ,

Sorry about the delayed response. From the logs it looks like your project is based on the MQTT mutual auth demo. I notice a difference between your code and the original demo. In the publishToTopic function in the original demo, there is a line that gets the next publishing index. It looks like this line is missing from your code. Can you try adding this line back? Please let us know if this works.

una801 commented 2 years ago

It was a very simple question. As you answered, I rewritten the public Toxic part while looking at the demo, and it was solved. While testing and modifying the code, I made these simple mistakes and got in trouble. Thanks for your help

una801 commented 2 years ago

Thanks