0

I am trying to send my measuring datas (temperature, humidity and pressure) by bluetooth and it has to be continuous so I am using threads for that but it is getting too complicated for me.

Here is the Bluetooth client code:

    #include "defines.h"

    void L2CAP_client(char *b_addr, float *temp, float *humi, float *pressure)
    {
        struct sockaddr_l2 addr = { 0 };

        struct Measurement_Data *Thread_Data = malloc(sizeof(struct Measurement_Data));
        int s, status;
        char dest[18];

        pthread_t Measurement_data_thread = 0;
        int  iret = 0;
        pthread_t Thread_id;

        strncpy(dest, b_addr, 18);

        // allocate a socket
        s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
        Thread_Data->s_ptr = s;

        // set the connection parameters (who to connect to)
        addr.l2_family = AF_BLUETOOTH;
        addr.l2_psm = htobs(0x1001);
        str2ba( dest, &addr.l2_bdaddr );

        // connect to server
        status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

        Thread_Data->temp = *temp;
        Thread_Data->humi = *humi;
        Thread_Data->pressure = *pressure;

        // send a message
        if( status == 0 ) {

            iret = pthread_create(&Measurement_data_thread, NULL, &Thread_Function,    (void*) &Thread_Data);
        if(iret != 0){
        perror("Thread creation failed\n");
       }
    Thread_id = pthread_self();
    printf("ID of Temp_thread is: %u\n", (unsigned int)Thread_id);

}

    if( status < 0 ) perror("uh oh");

    printf("Thread 1 returns: %d\n",iret);

    pthread_join(Measurement_data_thread, NULL);


    close(s);
 }

void *Thread_Function(void* Measurement_Data)
{
    int s = 0;
    struct Measurement_Data *Thread_Data = (struct Measurement_Data*) Measurement_Data;

    s = Thread_Data->s_ptr;
    send(s, &Thread_Data, sizeof(Thread_Data), 0);
    free(Thread_Data);

    //return 0;
}

Here is the Bluetooth server code:

#include "defines.h"

struct Measurement_Data *L2CAP_server(void)
{
    struct sockaddr_l2 loc_addr = { 0 }, rem_addr = { 0 };
    char buf[256] = { 0 };
    int s, client, bytes_read;
    int opt = sizeof(rem_addr);
    static struct Measurement_Data Data;
    struct Measurement_Data *Thread_Data = malloc(sizeof(struct Measurement_Data));

    pthread_t Measurement_data_thread = 0;
    int  iret = 0;
    pthread_t Thread_id;

    // allocate socket
    s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);

    // bind socket to port 0x1001 of the first available
    // bluetooth adapter
    loc_addr.l2_family = AF_BLUETOOTH;
    loc_addr.l2_bdaddr = *BDADDR_ANY;
    loc_addr.l2_psm = htobs(0x1001);

    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

    // put socket into listening mode
    listen(s, 1);

    // accept one connection
    client = accept(s, (struct sockaddr *)&rem_addr, &opt);
    Thread_Data->c_ptr = client;

    ba2str( &rem_addr.l2_bdaddr, buf );
    fprintf(stderr, "accepted connection from %s\n", buf);

    memset(buf, 0, sizeof(buf));

    iret = pthread_create(&Measurement_data_thread, NULL, &Thread_Function, (void*) &Thread_Data);
    if(iret != 0){
        perror("Thread creation failed\n");
    }
    Thread_id = pthread_self();
    printf("ID of Temp_thread is: %u\n", (unsigned int)Thread_id);


    printf("Thread 1 returns: %d\n",iret);

    pthread_join(Measurement_data_thread, NULL);

    // close connection
    close(client);
    close(s);

    return (&Data);

}
void *Thread_Function(void* Measurement_Data)
{
    int c = 0;
    struct Measurement_Data *Thread_Data = (struct Measurement_Data*) Measurement_Data;

    c = Thread_Data->c_ptr;
    recv(c, &Thread_Data, sizeof(Thread_Data), 0);
    printf("Temp: %0.1f\n", Thread_Data->temp);
    printf("Humi: %0.1f\n", Thread_Data->humi);
    printf("Pres: %0.1f\n", Thread_Data->pressure);
    free(Thread_Data);

    //return 0;
}

It is sending just zeros and I am also getting this error: * glibc detected * ./anturi_luku: munmap_chunk(): invalid pointer: 0xbef48738 ***

Am I right that I need to use mutex to lock the data?

jakeheik90
  • 11
  • 1
  • 6

4 Answers4

0

Valgrind is a very helpful tool in such scenarios. Were you able to use Valgrind ? Can you share the valgrind report.

Ensure to use the 'db-attach' option as Valgrind will pause after every error shown and print the line. Try using the following command by replacing the a.out with your filename.

$ valgrind --tool=memcheck --db-attach=yes ./a.out

Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
  • Thank you for fast reply! Is there any other ways to handle this situation because the valgrind isn't obviously working on arm architecture. I am getting this error: valgrind: failed to start tool 'memcheck' for platform 'arm-linux': No such file or directory – edit: There seems to be valgrind for Raspberry Pi too. – jakeheik90 Oct 10 '14 at 13:34
0

Here is the valgrind report from the client side:

==2157== Memcheck, a memory error detector
==2157== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==2157== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==2157== Command: ./anturi_luku
==2157== 
Temperature: 22.7C
Humidity: 61.7%
Temperature: 22.7C
Humidity: 61.7%
pressure= 0, 0, 0
Pressure: 0.0 Pa
ID of Temp_thread is: 67241968
Thread 1 returns: 0
==2157== Thread 2:
==2157== Invalid free() / delete / delete[] / realloc()
==2157==    at 0x48348B8: free (vg_replace_malloc.c:427)
==2157==    by 0x8C0B: Thread_Function (L2CAP_client.c:89)
==2157==    by 0x485FBFB: start_thread (pthread_create.c:306)
==2157==    by 0x4962967: ??? (clone.S:116)
==2157==  Address 0xbde575e8 is on thread 1's stack
==2157== 
==2157== 
==2157== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- n
==2157== 
==2157== HEAP SUMMARY:
==2157==     in use at exit: 16 bytes in 1 blocks
==2157==   total heap usage: 2 allocs, 2 frees, 152 bytes allocated
==2157== 
==2157== LEAK SUMMARY:
==2157==    definitely lost: 16 bytes in 1 blocks
==2157==    indirectly lost: 0 bytes in 0 blocks
==2157==      possibly lost: 0 bytes in 0 blocks
==2157==    still reachable: 0 bytes in 0 blocks
==2157==         suppressed: 0 bytes in 0 blocks
==2157== Rerun with --leak-check=full to see details of leaked memory
==2157== 
==2157== For counts of detected and suppressed errors, rerun with: -v
==2157== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)
jakeheik90
  • 11
  • 1
  • 6
0

Glad that you are able to get the Valgrind report for the bluetooth L2CAP client in C language over ARM.

As you can see, you will be asked the following when the first error occurs.

---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ----

Pressing Ret, or N Ret or n Ret, causes Valgrind not to start a debugger for this error.

Pressing Y Ret or y Ret causes Valgrind to start a debugger for the program at this point.

From the logs, it is observed that 'n' is used. Were you able to try with 'Y' ?

Also, from the logs it seems to convey that invalid free is done. Inline to it, it appears that free() is called on an un-allocated memory. Check it.

Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
  • This is weird because I am allocating memory two times and also I free it two times as you can see from my code. Am I doing the allocating or freeing wrong then? – jakeheik90 Oct 12 '14 at 10:19
0

Now I got that error message to disappear. It was that pthread_create function's fourth parameter. I removed the ampersand character.Now the function looks like this: pthread_create(&Measurement_data_thread, NULL, &Thread_Function, (void*) Thread_Data);

But it isn't still sending or receiving any data. When I use printf to check those data values to be sent on the sending ends thread_function they are fine. But on the reveiving ends it sometimes prints just zeros and sometimes nothing.

Do I need the mutex now to lock the data?

jakeheik90
  • 11
  • 1
  • 6