ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | LICENSE

ogl_beamformer_pipe.c (2224B)


      1 #include <mex.h>
      2 
      3 #include <stddef.h>
      4 #include <stdint.h>
      5 typedef int16_t   i16;
      6 typedef int32_t   i32;
      7 typedef ptrdiff_t size;
      8 
      9 #if defined(__unix__)
     10 #include <fcntl.h>
     11 #include <sys/stat.h>
     12 #include <unistd.h>
     13 
     14 #define OS_INVALID_FILE (-1)
     15 typedef i32 os_file;
     16 typedef struct {
     17 	os_file  file;
     18 	char    *name;
     19 } os_pipe;
     20 
     21 static os_pipe
     22 os_open_named_pipe(char *name)
     23 {
     24 	return (os_pipe){.file = open(name, O_WRONLY), .name = name};
     25 }
     26 
     27 static size
     28 os_write_to_pipe(os_pipe p, void *data, size len)
     29 {
     30 	size written = 0, w = 0;
     31 	do {
     32 		written += w;
     33 		w = write(p.file, data, len);
     34 	} while(written != len && w != 0);
     35 	return written;
     36 }
     37 
     38 static void
     39 os_close_pipe(os_pipe p)
     40 {
     41 	close(p.file);
     42 }
     43 
     44 #elif defined(_WIN32)
     45 #include <windows.h>
     46 
     47 #define OS_INVALID_FILE (INVALID_HANDLE_VALUE)
     48 typedef HANDLE os_file;
     49 typedef struct {
     50 	os_file  file;
     51 	char    *name;
     52 } os_pipe;
     53 
     54 static os_pipe
     55 os_open_named_pipe(char *name)
     56 {
     57 	HANDLE pipe = CreateFileA(name, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
     58 	return (os_pipe){.file = pipe, .name = name};
     59 }
     60 
     61 static size
     62 os_write_to_pipe(os_pipe p, void *data, size len)
     63 {
     64 	DWORD bytes_written;
     65 	WriteFile(p.file, data, len, &bytes_written, 0);
     66 	return bytes_written;
     67 }
     68 
     69 static void
     70 os_close_pipe(os_pipe p)
     71 {
     72 	CloseHandle(p.file);
     73 }
     74 
     75 #else
     76 #error Unsupported Platform
     77 #endif
     78 
     79 /* NOTE: usage: pipe_data_to_beamformer(pipe_name, data) */
     80 void
     81 mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
     82 {
     83 	if (nrhs != 2) {
     84 		mexPrintf("usage: ogl_beamformer_pipe(pipe_name, data)\n");
     85 		return;
     86 	}
     87 
     88 	char *pipe_name       = mxArrayToString(prhs[0]);
     89 	const mxArray *mxdata = prhs[1];
     90 
     91 	os_pipe p = os_open_named_pipe(pipe_name);
     92 	if (p.file == OS_INVALID_FILE) {
     93 		mexPrintf("ogl_beamformer_pipe: failed to open pipe\n");
     94 		return;
     95 	}
     96 
     97 	if (!mxIsInt16(mxdata)) {
     98 		os_close_pipe(p);
     99 		mexPrintf("ogl_beamformer_pipe: invalid data type; only int16 is supported\n");
    100 		return;
    101 	}
    102 
    103 	void *data     = mxGetPr(mxdata);
    104 	size data_size = mxGetNumberOfElements(mxdata) * sizeof(i16);
    105 	size written   = os_write_to_pipe(p, data, data_size);
    106 	if (written != data_size)
    107 		mexPrintf("ogl_beamformer_pipe: failed to write full data to pipe: wrote: %ld\n", written);
    108 
    109 	os_close_pipe(p);
    110 }