cbluetoothpopenobex

Can Anybody Explain me popen() with write mode


Here i want to Execute one Command and i have to give input to this command after executing first one.

i want to execute obex_test command for bluetooth mode than after i have to give input like 's' for starting server so how can i give this thing. here is my code i write this thing and got output. i got error of input data after executing obex_test command.

Code:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

void main() {

    char *input = "obex_test -b";   
    FILE *fp = NULL;
    char path[512];


    fp = popen(input, "w");

    if (fp == NULL) 
    {
        printf("\nFailed command\n");
        return;
    }
    else
    {
        printf("\nSuccesss command\n");
    }
    printf("starting while : %d", fp);

    while (fgets(path, sizeof(path) - 1, fp) != NULL) {

        printf("\nOutput    :::  %s \n", path);
    }

    printf("\nEnd\n");
    /* close */
    pclose(fp);

}

output :

Successs command
starting while : 69640
End
Using Bluetooth RFCOMM transport
OBEX Interactive test client/server.
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command

From Output after this line OBEX Interactive test client/server. i have to give input character 's' but i cant able to do this direclty its goes infinite loop and printf >Unknown command.


Solution

  • oh if you want to give input to popen command by your c file then try this way

    fputc ( 's', fp );
    fputc ( '\n', fp);
    

    here if you want to give s option then write 's'

    fp is file pointer of popen()

    it works fine

    in your code :

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <fcntl.h>
    
    void main() {
    
        char *input = "obex_test -b";   
        FILE *fp = NULL;
        char path[512];
    
    
        fp = popen(input, "w");
    
        if (fp == NULL) 
        {
            printf("\nFailed command\n");
            return;
        }
        else
        {
            printf("\nSuccesss command\n");
        }
    
    //giving s option to menu
    fputc ( 's', fp );
    fputc ( '\n', fp);
    
    
    
        printf("starting while : %d", fp);
    
        while (fgets(path, sizeof(path) - 1, fp) != NULL) {
    
            printf("\nOutput    :::  %s \n", path);
        }
    
        printf("\nEnd\n");
        /* close */
        pclose(fp);
    
    }
    

    Edit: to overcome for infine loop

    every time give two new line character after giving any option

    like

    //giving s option to menu
    fputc ( 's', fp );
    fputc ( '\n', fp);
    fputc ( '\n', fp);