Untitled

Java:

public static byte[] reverse(byte[] arr)
{
    byte[] buf = new byte[arr.length];
    int len = arr.length;
    for (int i = 0; i < len; i++) {
        buf[i] = arr[len - 1 - i];
    }
    return buf;
}

public void set_facediff(float value) 
{
    byte[] buf = reverse(ByteBuffer.allocate(4).putFloat(value).array());
    byte[] cmd = new byte[1];
    cmd[0] = 0x00;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        bos.write(cmd);
        bos.write(buf);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    byte[] iobuf = bos.toByteArray();
    ioctl(ctx, iobuf);

}

public float get_facediff()
{
    float facediff = 0.0f;
    byte[] cmd = new byte[5];
    cmd[0] = 0x01;
    ioctl(ctx, cmd);

    byte[] b = reverse(Arrays.copyOfRange(cmd, 1,cmd.length));
    ByteBuffer buffer = ByteBuffer.wrap(b);
    facediff = buffer.getFloat();

    return facediff;
}

Python:

import ctypes                                                                                                                              
import sys                                                                                                                                 
                                                                                                                                           
inputf = sys.argv[1]                                                                                                                       
data = open(inputf,'rb').read()                                                                                                            
                                                                                                                                           
lib = ctypes.CDLL('/root/demo/functions.so') 
lib.f(data,len(data))                                                                                                                      
                                                                                                                                           
print("\n*** END ***\n")   

C:

#include <stdio.h>                                                                                                                         
#include <unistd.h>                                                                                                                        
                                                                                                                                           
int f(unsigned char* buf, int len)                                                                                                         
{                                                                                                                                          
    printf("\n*** fast computation inside C ***\n");                                                                                       
    sleep(3);                                                                                                                              
                                                                                                                                           
    for (int i = 0; i < len; i++) {                                                                                                        
        printf("%02x ",buf[i]);                                                                                                            
        if ((i % 16) == 0) {                                                                                                               
            printf("\n");                                                                                                                  
        }                                                                                                                                  
    }                                                                                                                                      
    printf("\n");                                                                                                                          
    fflush(stdout);                                                                                                                                                
    return len;                                                                                                                            
}   

Don't forget to flush the remaining bytes less than 16!

Last updated

Was this helpful?