websocket_server.c 4.35 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libwebsockets.h>

#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <linux/serial.h>
#include <math.h>
#include "serial.h"


#define MAX_FRAME_SIZE  1024
#define WAIT_DELAY      50

#define         SERIAL_DEVICE           "/dev/ttyUSB0"




static int callback_http(
  struct libwebsocket_context *this,
  struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,
  void *user,void *in,size_t len)
{
return 0;
}




static int callback_my(
  struct libwebsocket_context * this,
  struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,
  void *user,void *in,size_t len)
{
static char *mesage=NULL;
static int msize=0;
switch(reason)
	{
  case LWS_CALLBACK_ESTABLISHED:
    printf("connection established\n");
    mesage=NULL;
                
                // Declenchement d'un prochain envoi au navigateur
    libwebsocket_callback_on_writable(this,wsi);
    break;
  
  case LWS_CALLBACK_RECEIVE:
                
                // Ici sont traites les mesages envoyes par le navigateur
    char* recei=(char *)in;
    int rec = *recei - 48; //réception de l'instruction, convertie en entier
    printf("received data: %d\n",rec);
    mesage=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
    if(mesage==NULL)
    {
    	perror("callback_my.malloc"); 
    	exit(EXIT_FAILURE);
    }
    char value[3];
    sprintf (value, "%d", (10*serial_read(&rec))+rec); //réception de la valeur demandée, sous forme de str
    printf("value = %s\n",value);
    memcpy(mesage+LWS_SEND_BUFFER_PRE_PADDING,value,1 + strlen(value));
                
                // Declenchement d'un prochain envoi au navigateur
    msize=strlen(value);
    libwebsocket_callback_on_writable(this,wsi);
    break;
    
  case LWS_CALLBACK_SERVER_WRITEABLE:
                // Ici sont envoyes les mesages au navigateur
    if(mesage!=NULL)
    {
      char *out=mesage+LWS_SEND_BUFFER_PRE_PADDING;
      printf("%s\n",out);
      printf("%d\n",msize);
      libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT);
      free(mesage);
      mesage=NULL;
    }
    break;
    
  default:
    break;
  }
return 0;
}




static struct libwebsocket_protocols protocols[] = 
{
  {
    "http-only",   // name
    callback_http, // callback
    0,             // data size
    0              // maximum frame size
  },
  {"myprotocol",callback_my,0,MAX_FRAME_SIZE},
  {NULL,NULL,0,0}
};




////
// SERIAL FUNCTIONS
////

//
// Open serial port device
//
int serialOpen(char *device,int mode){
int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR));
int fd=open(device,flags|O_NOCTTY); 
if(fd<0){ perror(device); exit(-1); }
return fd;
}

//
// Serial port configuration
//
void serialConfig(int fd,int speed){
struct termios new;
bzero(&new,sizeof(new));
new.c_cflag=CLOCAL|CREAD|speed|CS8;
new.c_iflag=0;
new.c_oflag=0;
new.c_lflag=0;     /* set input mode (non-canonical, no echo,...) */
new.c_cc[VTIME]=0; /* inter-character timer unused */
new.c_cc[VMIN]=1;  /* blocking read until 1 char received */
if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); }
}

//
// Serial port termination
//
void serialClose(int fd){
close(fd);
}


////
// User Functions
////
int serial_read(int *a)
{
int c=*a;
int i=0;
int n=0;
int value=0;
int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH);
serialConfig(sd,B9600);
if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } //Envoi de l'instruction

//Read the number of decimals
if(read(sd,&n,sizeof(char))!=1){ perror("main.read"); exit(-1); }
printf("%d\n",n-48);
n=n-48;

//Read and add all decimals
for (i=0;i<n;i++)
	{
	if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); }
	value=value+(c-48)*pow(10,n-i-1);
	}
serialClose(sd);

return value;
//printf("%d\n",value);

exit(0);
}









int main(void) 
{
int port=9000;
struct lws_context_creation_info info;
memset(&info,0,sizeof info);
info.port=port;
info.protocols=protocols;
info.gid=-1;
info.uid=-1;
struct libwebsocket_context *context=libwebsocket_create_context(&info);
if(context==NULL)
	{
  fprintf(stderr, "libwebsocket init failed\n");
  return -1;
  }
printf("starting server...\n");
while(1)
	{
  libwebsocket_service(context,WAIT_DELAY);
  }
libwebsocket_context_destroy(context);
return 0;
}