一、在inputCommander.h中
#include<wiringPi.h>
#include<stdio.h>
struct InputComamnder
{
char commandName[128];
char command[32];
char deviceName[128];
int (*Init)(struct InputCommander *socketMes, char *ipAddress, char *port);
int (*GetCommand)(struct InputCommander *socketMes);
char log[1024];
int fd;
struct InputCommander *next;
};
struct InputCommander *addvoiceControlToInputCommandLink(struct InputCommander* phead);
struct InputCommander *addsocketControlToInputCommandLink(struct InputCommander* phead);
2.在voiceControl.c中
#include<wiringPi.h>
#include<wiringSerial.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"inputCommand.h"
#include<unistd.h>
int voiceInit(struct InputCommander *voicer, char *ipAddress, char *port)
{
int fd;
fd = serialOpen(voicer->deviceName, 9600);
if(fd == -1)
{
exit(-1);
}
voicer->fd = fd;
return fd;
}
int voiceGetCommand(struct InputCommander *voicer)
{
int nread;
nread = read(voicer->fd, voicer->command, sizeof(voicer->command));
if(nread == 0)
{
printf("usart for voice read overtime");
}
else{
return nread;
}
}
struct InputCommander voiceControl = {
.commandName = "voice",
.deviceName = "/dev/ttyAMA0",
.command = {'\0'},
.Init = voiceInit,
.GetComamnd = voiceGetCommand,
.log = {'\0'},
.next = NULL
};
struct InputCommander *addvoiceControlToInputCommandLink(struct InputCommander *phead)
{
if(phead ==NULL)
{
return &voiceControl;
}
else{
voiceControl.next = phead;
phead = &voiceControl;
}
}
在sockerControl.c中
#include<wiringPi.h>
#include<wiringSerial.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"inputCommand.h"
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int socketInit (struct InputCommander *socketMes,char *ipAddress,char *port)
{
int s_fd;
int sfd;
struct sockaddr_in s_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(socketMes->port));
inet_aton(socketMes->ipAddress,&s_addr.sin_addr);
//2.bind
bind(s_fd,(struct sockaddr*) &s_addr, sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
printf("socket Server listening ......\n");
socketMes->sfd = s_fd;
return s_fd;
}
int socketGetCommand(struct InputCommander *socketMes)
{
int nread = 0;
int c_fd;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int clen = sizeof(struct sockaddr_in);
c_fd = accept(socketMes->sfd,(struct sockaddr*)&c_addr,&clen);
nread = read(c_fd, socketMes->command, sizeof(socketMes->command));
if(nread == -1)
{
perror("read");
}
else if(nread>0){
printf("int:\n",nread);
}
else{
printf("clent quit\n");
}
return nread;
}
struct InputCommander socketControl = {
.commandName = "socketServer",
.command = {'\0'},
.port = "8088",
.ipAddress = "192.168.3.46",
.Init = socketInit,
.GetCommand = socketGetCommand,
.log = {'\0'},
.next = NULL
};
struct InputCommander *addsocketControlToInputCommandLink(struct InputCommander* phead)
{
if(phead == NULL)
{
return &socketControl;
}
else{
socketControl.next = phead;
phead = &socketControl;
}
};
在mainPro.c中
#include<stdio.h>
#include"contrlDevices.h"
#include<string.h>
#include"inputCommand.h"
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>
struct Devices *pdeviceHead = NULL;//定义三个链表头
struct InputCommander *pCommandHead = NULL;
struct InputCommander *socketHandler = NULL;
int c_fd;
struct Devices *findDeviceByName(char *name,struct Devices *phead)
{
struct Devices *tmp = phead;
if(tmp == NULL)
{
return NULL;
}
else {
while(tmp != NULL)
{
if(strcmp(tmp->deviceName,name)== 0)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
struct InputCommander *findCommandByName(char *name,struct InputCommander *phead)
{
struct InputCommander *tmp = phead;
if(phead == NULL)
{
return NULL;
}
else {
while(tmp != NULL)
{
if(strcmp(tmp->commandName,name)== 0)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
void*voice_thread(void* datas)
{
struct InputCommander * voiceHandler;
int nread;
voiceHandler = findCommandByName("voice", pCommandHead);
if(voiceHandler == NULL)//在pCommandHead中有没有找到voice节点
{
printf("find voiceHandler error\n");
pthread_exit(NULL);
}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 datas\n");
}
else{
printf("datas is:\n",voiceHandler->command);
}
}
}
}
void* read_thread(void* datas)
{
int nread;
nread = read(c_fd, socketHandler->command, sizeof(socketHandler->command));
if(nread == -1)
{
perror("read");
}
else if(nread>0){
printf("int:\n",nread);
}
else{
printf("clent quit\n");
}
}
void* socket_thread(void* datas)
{
int c_fd;
//int nread;
pthread_t readThread;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int clen = sizeof(struct sockaddr_in);
socketHandler = findCommandByName("socketServer",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,&clen);
pthread_create(&readThread,NULL,read_thread,NULL);
}
}
int main()
{
char name[128];
struct Devices *tmp = NULL;
pthread_t voiceThread;
pthread_t socketThread;
if(-1==wiringPiSetup())
{
return -1;
}
pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
pdeviceHead = addfireIfOrNotToDeviceLink(pdeviceHead);
pCommandHead = addvoiceControlToInputCommandLink(pCommandHead);
pCommandHead = addsocketControlToInputCommandLink(pCommandHead);
//璇煶绾跨▼
pthread_create(&voiceThread,NULL,voice_thread,NULL);
//socket绾跨▼
pthread_create(&socketThread,NULL,socket_thread,NULL);
pthread_join(voiceThread,NULL);
pthread_join(socketThread,NULL);
while(1){
printf("input\n");
scanf("%s",name);
tmp = findDeviceByName(name,pdeviceHead);
if(tmp!=NULL){
tmp->deviceInit(tmp->pinNum);
tmp->open(tmp->pinNum);
}
}
}