testServeur.c 3.59 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libwebsockets.h>
#include <stdbool.h>
#include <unistd.h>
#include "serial.c"
#define MAX_FRAME_SIZE  1024
#define WAIT_DELAY      50

#define SERIAL_DEVICE "/dev/ttyAMA0"

int sd;
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 *message=NULL;
static int msize=0;
static char oldC = 0;
static  bool boole = 1;
char * input = "a";
char *out;
char c;

message=malloc(1+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
out=message+LWS_SEND_BUFFER_PRE_PADDING;
    if(message==NULL){ perror("callback_my.malloc"); exit(EXIT_FAILURE); }
    memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,(void*)input,1);
                // Declenchement d'un prochain envoi au navigateur
    msize=1;
switch(reason){
  case LWS_CALLBACK_ESTABLISHED:
    printf("connection established\n");
    message=NULL;
                // Declenchement d'un prochain envoi au navigateur
    libwebsocket_callback_on_writable(this,wsi);
    break;
  case LWS_CALLBACK_SERVER_WRITEABLE:
                // Ici sont envoyes les messages au navigateur

      
	printf("Debut lecture\n");
      read(sd,&c,sizeof(char));
	printf("%d\n",c);
	

      if(oldC != c){
        oldC = c;

        switch(c)
        {
	  case 0x00 : system("sudo cp /var/www/Sound_8b_Silence.wav /var/www/sound.wav");break;
          case 0x01 : system("sudo cp /var/www/Sound_8b_Do.wav /var/www/sound.wav");break;
          case 0x02 : system("sudo cp /var/www/Sound_8b_Re.wav /var/www/sound.wav");break;
          case 0x04 : system("sudo cp /var/www/Sound_8b_Mi.wav /var/www/sound.wav");break;
          case 0x08 : system("sudo cp /var/www/Sound_8b_Fa.wav /var/www/sound.wav");break;
          case 0x10 : system("sudo cp /var/www/Sound_8b_Sol.wav /var/www/sound.wav");break;
          case 0x20 : system("sudo cp /var/www/Sound_8b_La.wav /var/www/sound.wav");break;
          case 0x40 : system("sudo cp /var/www/Sound_8b_Si.wav /var/www/sound.wav");break;
          case 0x80 : system("sudo cp /var/www/Sound_8b_DoAigu.wav /var/www/sound.wav");break;
          default :break;
        }

        libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT);

      }
      
/*
      if(boole){
        system("sudo cp soundLevel.mp3 ./sound.mp3");

      }else system("sudo cp Raindrops-noise.mp3 ./sound.mp3");

       libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT);

    }*/
      libwebsocket_callback_on_writable(this, wsi);
    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}
  };

int main(void) {
char *out="a"+LWS_SEND_BUFFER_PRE_PADDING;
int port=9000;
struct lws_context_creation_info info;
memset(&info,0,sizeof info);
sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH);
serialConfig(sd,B9600);

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;
}