socketContrl.c中 read不方便一致循环读取,暂时放到主程序中
主程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "contrlDevices.h"
#include "InputCommand.h"
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
//全局变量
struct InputCommander *pcommandHead = NULL;
struct Devices *pdeviceHead = NULL;
struct InputCommander *socketHandler =NULL;
int c_fd;
struct Devices *findDeviceByName(char *name,struct Devices *pdeviceHead)
{
struct Devices *tmp = pdeviceHead;
if(tmp == NULL)
{
return NULL;
}else
{
while(tmp != NULL)
{
if(strcmp(name,tmp->deviceName) == 0)
{
return tmp;
}
tmp = tmp->next;
}
}
}
struct InputCommander *findCommandByName(char *name,struct InputCommander *pcommandHead)
{
struct InputCommander *tmp = pcommandHead;
if(tmp == NULL)
{
return NULL;
}else
{
while(tmp != NULL)
{
if(strcmp(name,tmp->commandName) == 0)
{
return tmp;
}
tmp = tmp->next;
}
}
}
void *socketRead(void *data)
{
int n_read;
memset(socketHandler->command,'\0',sizeof(socketHandler->command));
n_read = read(c_fd,socketHandler->command,sizeof(socketHandler->command));
if(n_read == -1)
{
perror("read");
pthread_exit(NULL);
}if(n_read>0)
{
printf("\nget:%d:%s\n",n_read,socketHandler->command);
}else
{
printf("client quit\n");
}
}
void *socketThread(void *data)
{
struct sockaddr_in c_addr;
int c_len = sizeof(struct sockaddr_in);
pthread_t readThread;
socketHandler = findCommandByName("socket",pcommandHead);
if(socketHandler == NULL)
{
printf("find socketHandler error\n");
pthread_exit(NULL);
}else
{
printf("%s init success\n",socketHandler->commandName);
}
socketHandler->Init(socketHandler,NULL,NULL);
while(1)
{
c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr,&c_len);
pthread_create(&readThread,NULL,socketRead,NULL);
}
}
void *voiceThread(void *data)
{
int nread;
struct InputCommander *voiceHandler;
voiceHandler = findCommandByName("voice",pcommandHead);
if(voiceHandler == NULL)
{
printf("find voiceHandler error\n");
}else
{
if(voiceHandler->Init(voiceHandler,NULL,NULL) < 0)
{
printf("voice Init error\n");
pthread_exit(NULL);
}else
{
printf("%s init success\n",voiceHandler->commandName);
}
while(1)
{
nread = voiceHandler->getCommand(voiceHandler);
if(nread == 0)
{
printf("no data from voice\n");
}else
{
printf("do voice contrl:%s\n",voiceHandler->command);
}
}
}
}
int main()
{
pthread_t voice_thread;
pthread_t socket_thread;
if(-1 == wiringPiSetup())
{
return -1;
}
//设备工厂初始化
pdeviceHead = addBathroomLightToLink(pdeviceHead);
pdeviceHead = addupstairLightToLink(pdeviceHead);
pdeviceHead = addrestaurantLightToLink(pdeviceHead);
pdeviceHead = addlivingroomLightToLink(pdeviceHead);
pdeviceHead = addfireToLink(pdeviceHead);
bathroomLightInit(22);
livingroomLightInit(24);
restaurantLightInit(23);
upstairLightInit(21);
//指令工厂初始化
pcommandHead = addvoiceContrlToCommandLink(pcommandHead);
pcommandHead = addsocketContrlToCommandLink(pcommandHead);
//线程初始化
//语音
pthread_create(&voice_thread,NULL,voiceThread,NULL);
//服务器
pthread_create(&socket_thread,NULL,socketThread,NULL);
pthread_join(voice_thread,NULL);
pthread_join(socket_thread,NULL);
return 0;
}
voiceContrl.c
#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int voiceInit(struct InputCommander *voicer,char *ipAdress,char *port)
{
int fd;
if((fd = serialOpen(voicer->deviceName,9600)) == -1)
{
exit(-1);
}
voicer->fd = fd;
return fd;
}
int voiceGetCommand(struct InputCommander *voicer)
{
int nread = 0;
memset(voicer->command,'\0',sizeof(voicer->command));
nread = read(voicer->fd,voicer->command,sizeof(voicer->command));
return nread;
}
struct InputCommander voiceContrl = {
.commandName = "voice",
.command = {'\0'},
.deviceName = "/dev/ttyAMA0",
.Init = voiceInit,
.getCommand = voiceGetCommand,
.log = {'\0'},
.next = NULL
};
struct InputCommander *addvoiceContrlToCommandLink(struct InputCommander *phead)
{
if(phead == NULL)
{
return &voiceContrl;
}else
{
voiceContrl.next = phead;
phead = &voiceContrl;
return phead;
}
}
socketContrl.c
#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
int socketInit(struct InputCommander *socketMsg,char *ipAdress,char *port)
{
int s_fd;
struct sockaddr_in s_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
//socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1)
{
perror("socket");
exit(-1);
}
//bind
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(socketMsg->port));
inet_aton(socketMsg->socketAdress,&s_addr.sin_addr);
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//listen
listen(s_fd,10);
printf("socket Server Listening.......\n");
socketMsg->sfd = s_fd;
return s_fd;
}
int socketGetCommand(struct InputCommander *socketMsg)
{
int c_fd;
int n_read;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int c_len = sizeof(struct sockaddr_in);
c_fd = accept(socketMsg->sfd,(struct sockaddr *)&c_addr,&c_len);
n_read = read(c_fd,socketMsg->command,sizeof(socketMsg->command));
if(n_read == -1)
{
perror("read");
}if(n_read>0)
{
printf("\nget:%d\n",n_read);
}else
{
printf("client quit\n");
}
}
struct InputCommander socketContrl = {
.commandName = "socket",
.command = {'\0'},
.port = "8081",
.socketAdress = "192.168.1.176",
.Init = socketInit,
.getCommand = socketGetCommand,
.log = {'\0'},
.next = NULL
};
struct InputCommander *addsocketContrlToCommandLink(struct InputCommander *phead)
{
if(phead == NULL)
{
return &socketContrl;
}else
{
socketContrl.next = phead;
phead = &socketContrl;
return phead;
}
}