Thursday, 18 August 2016

BTLE GATT Services without Bluez

Unix domain UDP Socket (PHP to C++)

R-PI Preloaded SD Cards at 

Apache php, Linux:

2 unix socket (files) are required, one for C++ peer and one for the php. 
On each socket to other peer would send / receive. The image shows the concept. The files we choose are located in tmp folder.

 PHP code: (error checking was removed for code clarity)

$sock = socket_create (AF_UNIX, SOCK_DGRAM, 0 );
// create socket unix file
socket_bind ( $sock , "/tmp/phpfile" );
chmod("/tmp/phpfile", 0777);

// send some data to cpp
socket_sendto($sock, "hello", 5, 0, "/tmp/cppfile", 0);

// rec some data from anyone which sends to "/tmp/phpfile"

$from=""; $buf="";$port=0;
socket_recvfrom($sock, $buf, 32, 0, $from, $port);

 C++ Code:

socklen_t al = sizeof(struct sockaddr_un);
int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
struct sockaddr_un cpp_addr = {0};

// bind to receive
cpp_addr.sun_family = AF_UNIX;
::strcpy(cpp_addr.sun_path, "/tmp/cppfile");

// creates socket file
::bind(sfd, (const struct sockaddr *) &cpp_addr, sizeof(cpp_addr);
system("chmod 777 /tmp/cppfile");

//receive from anyone who sends to "/tmp/cppfile"
struct  sockaddr_un php_addr={0};
int al = sizeof(struct sockaddr_un);
char buff[64];

::recvfrom(sfd, buff, 63, 0,
           (struct sockaddr *) &(php_addr),

 // send to php
 al = sizeof(struct sockaddr_un);
 php_addr.un_family = AF_UNIX;
::strcpy( php_addr.sun_path,"/tmp/phpfile");

//make sure when this is called the php code is blocked in  socket_recvfrom

               5, 0,
               (struct sockaddr *) &(php_addr),



No comments:

Post a Comment