主函数Mainpro.c
#include <stdio.h>
#include <string.h>
#include "controlDevice.h"
#include "InputCommand.h"
#include <unistd.h>
struct Device *findDeviceByName(char *name,struct Device *phead)
{
struct Device* tmp = phead;
if(phead == 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;
}
}
};
int main()
{
if(wiringPiSetup() == -1){
printf("set up error!\n");
return -1;
}
struct Device *tmp = NULL;
struct Device *tmp1 = NULL;
char *name = "restaurant";
int frieStatus;
struct Device *pDeviceHead = NULL;
pDeviceHead = addrBathroomLight(pDeviceHead);
pDeviceHead = addrlivingRoomLight(pDeviceHead);
pDeviceHead = addrrestaurantLight(pDeviceHead);
pDeviceHead = addrsecondfloorLight(pDeviceHead);
pDeviceHead = addrFireBell(pDeviceHead);
pDeviceHead = addrBee(pDeviceHead);
struct inputcommander *pCommandHead = NULL;
pCommandHead = addrVoiceControl(pCommandHead);
pCommandHead = addrSocketControl(pCommandHead);
while(1){
tmp = findDeviceByName("Frie",pDeviceHead);
if(tmp != NULL){
tmp->DeviceInit(tmp->pinNum);
frieStatus = tmp->readStatus(tmp->pinNum);
tmp1 = findDeviceByName("Bee",pDeviceHead);
if(tmp1 != NULL){
tmp1->DeviceInit(tmp1->pinNum);
if(frieStatus == 0){
tmp1->DeviceInit(tmp1->pinNum);
tmp1->open(tmp1->pinNum);
sleep(3);
printf("fire\n");
}else{
tmp1->DeviceInit(tmp1->pinNum);
tmp1->close(tmp1->pinNum);
}
}
}
}
return 0;
}
控制管理InputCommand.h
#include <wiringPi.h>
#include <stdlib.h>
struct inputcommander
{
char commandName[128];
char deviceName[128];
char command[32];
int (*Init)(struct inputcommander *commander);
int (*getcommand)(struct inputcommander *commander);
int fd;
char port[18];
char ipaddr[32];
char log[1024];
int sfd;
struct inputcommander *next;
};
struct inputcommander *addrVoiceControl(struct inputcommander *phead);
struct inputcommander *addrSocketControl(struct inputcommander *phead);
语音Control-voice.c
#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int VoiceInit(struct inputcommander *commander)
{
int fd;
if((fd = serialOpen(commander->deviceName,115200))== -1){
exit(-1);
}else{
printf("serial open success\n");
}
commander->fd = fd;
return fd;
}
int VoiceGetCommand(struct inputcommander *commander)
{
int nread = 0;
nread = read(commander->fd,commander->command,sizeof(commander->command));
return nread;
}
struct inputcommander VoiceControl={
.commandName = "voice",
.deviceName = "/dev/ttyS5",
.command = {'\0'},
.Init = VoiceInit,
.getcommand = VoiceGetCommand,
.log = {'\0'},
.next = NULL
};
struct inputcommander *addrVoiceControl(struct inputcommander *phead)
{
if(phead == NULL){
return &VoiceControl;
}else{
VoiceControl.next = phead;
phead = &VoiceControl;
return phead;
}
};
网络Control-socket.c
#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int SocketInit(struct inputcommander *commander)
{
int s_fd;
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(commander->port));
inet_aton(commander->ipaddr,&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");
commander->sfd = s_fd;
return s_fd;
}
int SocketGetCommand(struct inputcommander *commander)
{
int c_fd;
int nread = 0;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int len = sizeof(struct sockaddr_in);
c_fd = accept(commander->sfd,(struct sockaddr *)&c_addr,&len);
memset(commander->command,0,sizeof(commander->command));
nread = read(c_fd,commander->command,sizeof(commander->command));
if(nread == -1){
perror("read");
}else if(nread > 0){
printf("get message:%d\n",nread);
}else{
printf("client quit\n");
}
return nread;
}
struct inputcommander SocketControl={
.commandName = "socket",
.command = {'\0'},
.Init = SocketInit,
.getcommand = SocketGetCommand,
.port = "8088",
.ipaddr = "192.168.0.107",
.log = {'\0'},
.next = NULL
};
struct inputcommander *addrSocketControl(struct inputcommander *phead)
{
if(phead == NULL){
return &SocketControl;
}else{
SocketControl.next = phead;
phead = &SocketControl;
return phead;
}
};