基于ARM的智能灯光控制系统(10)网络节点

节点设备

网络通信协议

//–head– cmd data crc
//0x5a 0x15 0x 0x 0x

协议头部(2字节) 命令位(1字节) 数据位(1字节) 校验位(1字节) 功能
0x5a 0x15 0x10 0x11 前四位求和 查询设备在线
0x5a 0x15 0x20 0x21 前四位求和 在线
0x5a 0x15 0x30 0x31 前四位求和 设备开
0x5a 0x15 0x30 0x32 前四位求和 设备关
0x5a 0x15 0x30 0x33 前四位求和 设备自动开关

主控端网络程序(net_pro.c)

net_por.h

#ifndef __SL2000_NET_H_
#define __SL2000_NET_H_
#define  DEF_PORT  6899

int socket_init(int port);
void* client_pro(void* arg);
void* net_pro(void* arg);
int send_cmd(char  cmd, char cmd_da ,int sockfd);
int get_cmd(char *buf,char* cmd,int len);
#endif
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <pthread.h>   

#include "config.h"
#include "net_pro.h"

int net_user;
unsigned char net_cmd;
unsigned char net_flag;

extern struct sys_all g_dev[1]; 
extern struct dev_fd g_dev_fd;//系统设备文件

int socket_init(int port)
{
    int sockfd;
    struct sockaddr_in server_addr;
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
            fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
            exit(1);
    }
    printf("socket...\n");
    bzero(&server_addr,sizeof(struct sockaddr_in));
    server_addr.sin_family=AF_INET;
    server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    server_addr.sin_port=htons(port);
    if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
    {
            fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
            exit(1);
    }
    printf("bind...\n");
    if(listen(sockfd,10)==-1)
    {
            fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
            exit(1);
    }
    printf("listen...\n");

    return sockfd;
}


//protocol 
//--head--  cmd  data    crc    
//0x5a 0x15  0x     0x      0x
int get_cmd(char *buf,char* cmd,int len)
{
    char crc=0;
    int i;
    if(buf==NULL || cmd==NULL || len !=5 )
        return -1;
    if(buf[0] != 0x5a || buf[1] != 0x15)
        return -1;
    for(i=0;i<len-1;i++){
        crc += buf[i];
    }   
    if(crc != buf[len-1])
        return -1;
    cmd[0] = buf[2];
    cmd[1] = buf[3];
    return 0;
}


int send_cmd(char  cmd, char cmd_da ,int sockfd)
{
    unsigned char buf[8];
    if(sockfd < 0)
        return -1;
    buf[0] = 0x5a;
    buf[1] = 0x15;
    buf[2] = cmd;
    buf[3] = cmd_da;
    buf[4] = buf[0] + buf[1] + buf[2] + buf[3];
    if(send(sockfd,buf,5,0)==-1){
        fprintf(stderr,"Write Error:%s\n",strerror(errno));
        return -1;
    }
    return 0;
}


void* client_pro(void* arg)
{
    int fd,*p;
    int len,index;
    int timer=0;
    unsigned char recv_buf[8],cmd[8],old_cmd;   

    p=(void*)arg;
    fd = *p;
    net_user++; 
    index = net_user - 1;
    net_flag= 1;//在线
    g_dev_fd.net_fd[index] = fd;
    old_cmd = net_cmd;

    while(1){
        bzero(recv_buf,8);
        bzero(cmd,8);
        if(timer == 3){
            timer = 0;
            send_cmd(0x10, 0x11, fd);   //查询设备在线    
            len = recv(fd,recv_buf,8,0);
            if(len==0 || len==-1){
                net_user--;
                close(fd);
                net_flag = 0;//下线
                g_dev_fd.net_fd[index] = -1;
                pthread_exit("0");
            }else{
                get_cmd(recv_buf,cmd, len);
                if(cmd[0]==0x20 && cmd[1]==0x21)
                    net_flag = 1;
                else
                    net_flag = 0;

                //printf("\nbuf=%x,%x,%x,%x,%x=cmd=%x,%x=\n",
                //  recv_buf[0],recv_buf[1],recv_buf[2],recv_buf[3],
                //  recv_buf[4],cmd[0],cmd[1]);     
            }   
        }
        if(old_cmd != net_cmd){
            old_cmd = net_cmd;
            send_cmd(0x30, old_cmd,fd);
        }
        sleep(1);
        timer++;
    }
}

void* net_pro(void* arg)
{
        pthread_t pth;
        int sockfd,new_fd,sin_size;
        struct sockaddr_in client_addr;     

        printf("net server pthread start...\n");        
        sockfd = socket_init(DEF_PORT);   
        printf("sockfd=%d\n",sockfd);      
        while(1){
            printf("accept before...\n");   
            sin_size=sizeof(struct sockaddr_in);        
            new_fd=accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size);
            if(new_fd==-1){
                fprintf(stderr,"Accpet error:%s\n\a",strerror(errno));
                pthread_exit("0");
            }               
            fprintf(stderr,"Sever from %s\n",inet_ntoa(client_addr.sin_addr));          
            if(net_user<MAX_NET){       
                        pthread_create(&pth,NULL,client_pro,(void*)&new_fd);                        
            }
        }
}

节点端网络程序(net_light.c)

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<fcntl.h
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>

#define DEV_RELAY "/dev/4418_relay"
#define DEV_PIR "/dev/4418_pir"
#define J1_OFF  0x01
#define J1_ON   0x00
#define BUF_SIZE 32
int fd_relay=-1,fd_pir = -1;

int dev_init()
{
    printf("nanoPi driver init...\n");

    fd_relay = open(DEV_RELAY,O_RDWR);
    if(fd_relay<0){
        printf("open device err\n");
        return -1;
    }

    fd_pir = open(DEV_PIR,O_RDWR);
    if(fd_pir<0){
        printf("open device err\n");
        return -1;
    }
}

int net_init(char* ip ,char* port_str)
{
    int sockfd,port;
    struct sockaddr_in server_addr;

    port = atoi(port_str);
    if(port<1024){
        printf("port error\n"); 
        return -1;
    }

    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
        fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
        return -1;
    }

    bzero(&server_addr,sizeof(server_addr));
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(port);
    server_addr.sin_addr.s_addr=inet_addr(ip);

    if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1){
        fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
        return -1;
    }
    return sockfd;
}


void dev_sw(int sw)
{
    char da[2];
    if(sw == 3){
        read(fd_pir,da,1);
        write(fd_relay,da,1);
    }else{
        if(sw == 1)
            da[0] = J1_ON;
        if(sw == 2)
            da[0] = J1_OFF;
        write(fd_relay,da,1);
    }
    printf("==sw=%d==\n",sw);
}

//protocol 
//--head--  cmd  data    crc    
//0x5a 0x15  0x     0x      0x

int get_cmd(char *buf,char* cmd,int len)
{
    char crc=0;
    int i;
    if(buf==NULL || cmd==NULL || len !=5 )
        return -1;
    if(buf[0] != 0x5a || buf[1] != 0x15)
        return -1;
    for(i=0;i<len-1;i++){
        crc += buf[i];
    }   
    if(crc != buf[len-1])
        return -1;
    cmd[0] = buf[2];
    cmd[1] = buf[3];
    return 0;
}

int cmd_proc(char * cmd,int sockfd)
{
    char buf[8];    
    buf[0] = 0x5a;
    buf[1] = 0x15;
    if(cmd == NULL || sockfd < 0)
        return -1;

    if(cmd[0] == 0x10){
        buf[2] = 0x20;
        buf[3] = 0x21;
        buf[4] = buf[0] + buf[1] + buf[2] + buf[3];
        if(send(sockfd,buf,5,0)==-1){
            fprintf(stderr,"Write Error:%s\n",strerror(errno));
        }
    }
    if(cmd[0] == 0x30){     
        switch(cmd[1]){
            case 0x31://on
                dev_sw(1);
                break;
            case 0x32://off
                dev_sw(2);
                break;
            case 0x33://auto
                dev_sw(3);
                break;
        }
    }
    return 0;
}

int main(int argc, char *argv[])
{
    int sockfd = -1 ;
    int nbytes = 0;
    char recv_buf[BUF_SIZE],cmd[2]; 

    if(argc < 3){
        printf("please input server IP,PORT.\n");   
        exit(0);
    }
    if( dev_init() < 0)
        exit(0);
    if( (sockfd = net_init(argv[1],argv[2])) < 0)
        exit(0);

    while(1){
        bzero(recv_buf,BUF_SIZE);
        bzero(cmd,2);
        if((nbytes=recv(sockfd,recv_buf,BUF_SIZE,0))==-1){
            fprintf(stderr,"Read Error:%s\n",recv_buf);
            exit(1);
        }
        if(nbytes >= 5){
            if(get_cmd(recv_buf,cmd,nbytes)==0) 
                cmd_proc(cmd,sockfd);
        printf("\nbuf=%x,%x,%x,%x,%x=cmd=%x,%x=\n",
                recv_buf[0],recv_buf[1],recv_buf[2],recv_buf[3],
                recv_buf[4],cmd[0],cmd[1]);     
        }   
//      sleep(1);   

    }
    return 0;
}

Copyright © 2016 www.91arm.com 【91创客学堂】