安防监控 —— 主框架搭建
Posted y4247464
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安防监控 —— 主框架搭建相关的知识,希望对你有一定的参考价值。
一、原理验证代码
1、模块化编程
在最开始编程的时候,往往都会把主函数与被调函数都放在一个main文件中实现,但在实际开发中,要实现的功能很多,且关系复杂,那么在一个main.c中就会很繁杂,臃肿。
在实际项目开发中,一个复杂的项目需要小组间合作完成,每个人复杂一部分功能的开发。自己负责的模块,要将模块功能写好,封装成一个.c和.h ,然后交给项目组长,组长负责整体框架main的编写与每个模块的组合调试,最后各个模块组合,形成整个工程。
模块化编程核心:
a、将系统的各个功能进行封装成独立的模块,其他人只需要使用你所提供的函数和变量等,就可以完成相对应的功能
b、模块化的本质也就是,新建一个.c和.h文件,
.c文件里面存放着你所构建的函数,功能等,而当想让某个功能或函数可以被其他文件使用时,这时候便需要在对应的.H之中声明,
在外部调用时,只需要#include包含相对应的.h 即可
2、验证代码
模块化之前的main.c
实现加减乘除取模
1 #include "stdio.h" 2 3 int add(int x, int y); 4 int sub(int x, int y); 5 int mul(int x, int y); 6 int div(int x, int y); 7 int mod(int x, int y); 8 9 10 int main(int argc, const char *argv[]) 11 { 12 int a = 6; 13 int b = 4; 14 15 printf("a+b=%d ",add(a,b)); 16 printf("a-b=%d ",sub(a,b)); 17 printf("a*b=%d ",mul(a,b)); 18 printf("a/b=%d ",div(a,b)); 19 printf("a mod b=%d ",mod(a,b)); 20 21 return 0; 22 } 23 24 int add(int x, int y) 25 { 26 return x + y; 27 } 28 29 int sub(int x, int y) 30 { 31 return x - y; 32 } 33 34 int mul(int x, int y) 35 { 36 return x * y; 37 } 38 39 int div(int x, int y) 40 { 41 return x + y; 42 } 43 44 int mod(int x, int y) 45 { 46 return x % y; 47 }
模块化:将加、减、乘、除、取模各个功能分别定义在不同文件中,将各个函数在.h文件中声明。如果加减乘除等文件都需要调用一个功能函数A,可以将公共调用的功能A定义在command.c中,并在调用A的文件中,进行外部声明,包含其头文件。
1 #include "common.h" 2 3 int main(int argc, const char *argv[]) 4 { 5 int a = 6,b = 4; 6 7 printf("add: %d. ",add(a,b)); 8 printf("sub: %d. ",sub(a,b)); 9 printf("mul: %d. ",mul(a,b)); 10 printf("div: %d. ",div(a,b)); 11 printf("mod: %d. ",mod(a,b)); 12 13 return 0; 14 }
1 #include "common.h" 2 3 int printf_str(char *str) 4 { 5 //printf("---%s---%d. ",__func__,__LINE__); 6 printf("*************************. "); 7 printf("printf_str :%s",str); 8 9 return 0; 10 }
1 #ifndef __COMMON_H_ 2 #define __COMMON_H_ 3 4 #include <stdio.h> 5 6 extern int add(int a,int b); 7 extern int sub(int a,int b); 8 extern int mul(int a,int b); 9 extern int div(int a,int b); 10 extern int mod(int a,int b); 11 extern int printf_str(char *str); 12 13 #endif
1 #include "common.h" 2 3 extern int printf_str(char *str); 4 5 int add(int a,int b) 6 { 7 printf_str("add called. "); 8 return a + b; 9 }
1 CC=gcc 2 CFLAGS= -g -Wall 3 4 src=$(wildcard *.c) 5 obj=$(patsubst %.c,%.o,$(src)) 6 7 all:$(obj) 8 $(CC) $(CFLAGS) *.o -o example 9 %.o:%.c 10 $(CC) $(CFLAGS) -c $< -o $@ 11 12 .PHONY:clean 13 clean: 14 rm *.o
....div.c mul.c mod.c sub.c ...
同样,我们也使用模块化的思想来构建我们的框架
/**************头文件(XX.h)注意事项:************/ //1.函数默认是extern属性 也就是我们声明函数的时候前面的extern可有可无 extern void LED_Open(); void LED_Open(); //相同 //2.“.h”文件中不可以定义变量 在.h中只能声明,不能定义 #ifndef LED.h #define LED.h extern a; //声明变量a 正确 b=3; //定义变量b 错误 #endif //3、声明变量不会占用内存,定义变量才会 定义变量和声明变量的区别在于定义会产生分配内存的操作,这是汇编阶段的概念; //声明则只是告诉包含该声明的模块在连接阶段从其他模块寻找外部函数和变量。 //4、不想让外界知道的信息,就不应该出现在头文件里,而想让外部调用的函数或变量,则一定要在头文件中声明 //5、头文件(.h)命名应与源文件(.c)一致,便于清晰的查看各个头文件 //、6 #include <stdio.h>,#include "myfile.h",双引号先在工程目录里寻找,再去系统目录里寻找。 /**************.c文件(XX.c)注意事项:************/ //1.模块内不想被外部引用的函数和全局变量需在“.c”文件头冠以static关键字声明。 //这样这些函数和变量只会在当前.c文件中起到作用,一来可以避免函数名的重复;二来可以保护内部的实现数据,防止被破坏 //2、模块中想要被其他文件访问的变量,一定要是全局变量,并且在.h中声明 //3、要尽量减少全局变量的使用,因为全局变量的生命周期是从程序的开始到程序的结束的, //这意味着你在其他源文件中调用这个变量时,可能会产生同名变量,以及变量数值错误等问题 //4、函数的声明有无extern都行,变量的声明必须加上extern,否则编译器无法识别声明。
二、主框架搭建
看下软件框架流程图
1)可以看到LED、蜂鸣器、摄像头、Zigbee、数据库、数据刷新等都是独立的功能模块,由此我们可以将他们的功能实现分别定义在各自线程处理文件中。
2)对于中间的核心部分,主线程会对各个子线程的资源和任务进行分配,在主线程会处理哪些子线程需要上传数据并处理,会处理web层发来的控制命令
要分发给某个线程等事务,由此核心部分的代码在main.c中实现。
3)对于一些公共功能模块,我们将其定义在common.c文件中
1、具体要创建哪些文件
1)pthread_buzzer.c
A9蜂鸣器控制线程:实现打开蜂鸣器设备文件,使用ioctl接口控制蜂鸣器
2)pthread_led.c
A9LED模块线程:打开LED设备,通过ioctl控制LED灯
3)pthread_sqlite.c
A9数据库线程:用于保存数据,控制数据
4)pthread_transfer.c
数据采集线程:用于接收Zigbee数据和A9平台的传感器数据
5)pthread_refresh.c
数据刷新线程:将采集到的数据刷新到网页上显示
6)pthread_client_request.c
客户端命令接收线程:接收Web(客户端)的下发的命令请求
7)pthread_sms.c
GPRS线程:实现电话短信报警功能
8)data_global.c
公共资源:定义全局的互斥体、全局的条件锁、全局的id和key信息、
定义全局的消息队列发送函数
9)data_global.h
全局的宏定义#define、全局的线程函数声明、全局的设备节点声明
全局的消息队列发送函数外部extern声明、全局的消息队列传递的结构体信息声明
10)main.c
初始化锁与条件变量,创建线程,阻塞等待线程结束,释放锁与条件变量等线程资源
代码:
1 #include "data_global.h" 2 3 4 /********************************************************* 5 data_global.c : 6 定义全局的互斥体 7 定义全局的条件锁 8 定义全局的id和key信息 9 定义全局的消息队列发送函数 10 *********************************************************/ 11 12 //定义全局的互斥体,多个资源采用多把互斥锁 13 pthread_mutex_t mutex_client_request, 14 mutex_refresh, 15 mutex_sqlite, 16 mutex_transfer, 17 mutex_sms, 18 mutex_buzzer, 19 mutex_led; 20 21 //定义全局的条件变量 22 pthread_cond_t cond_client_request, 23 cond_refresh, 24 cond_sqlite, 25 cond_transfer, 26 cond_sms, 27 cond_buzzer, 28 cond_led; 29 30 //system V是针对IPC对象的ID操作的,而ID(标识符)是由key(键)生成的 31 //key_t ftok() ---> key ------> ID 32 int msgid; 33 int shmid; 34 int semid; 35 36 key_t key; //msg_key 37 key_t shm_key; 38 key_t sem_key; 39 40 //发送短信与接收短信的号码 41 char recive_phone[12] = {0}; 42 char center_phone[12] = {0}; 43 44 45 struct env_info_client_addr sm_all_env_info; //安防监控项目所有的环境信息 46 47 //全局的消息队列发送函数 48 int send_msg_queue(long type,unsigned char text) 49 { 50 struct msg msgbuf; 51 msgbuf.type = 1L; 52 msgbuf.msgtype = type; 53 msgbuf.text[0] = text; 54 55 if(msgsnd(msgid,&msgbuf,sizeof(msgbuf) - sizeof(long),0) == -1){ 56 perror("fail to msgsnd type2"); 57 exit(1); 58 } 59 60 return 0; 61 }
1 #ifndef __DATA_GLOBAL__H__ 2 #define __DATA_GLOBAL__H__ 3 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <fcntl.h> 9 #include <unistd.h> 10 #include <signal.h> 11 #include <pthread.h> 12 #include <termios.h> 13 #include <syscall.h> 14 15 #include <sys/ipc.h> 16 #include <sys/shm.h> 17 #include <sys/msg.h> 18 #include <sys/sem.h> 19 #include <sys/stat.h> 20 #include <sys/types.h> 21 #include <sys/ioctl.h> 22 #include <linux/fs.h> 23 #include <linux/ioctl.h> 24 #include <stdint.h> 25 #include <unistd.h> 26 #include <errno.h> 27 #include <linux/input.h> 28 29 30 /********************************************************* 31 data_global.h : 32 33 全局的宏定义#define 34 全局的线程函数声明 35 全局的设备节点声明 36 全局的消息队列发送函数外部extern声明 37 全局的消息队列传递的结构体信息声明 38 39 *********************************************************/ 40 41 42 /********************************************************* 43 全局的宏定义 44 *********************************************************/ 45 46 #define MONITOR_NUM 1 47 #define QUEUE_MSG_LEN 32 48 49 #define GPRS_DEV "/dev/ttyUSB0" 50 #define ZIGBEE_DEV "/dev/ttyUSB1" 51 #define BEEPER_DEV "/dev/fsbeeper0" 52 #define LED_DEV "/dev/fsled0" 53 54 55 /********************************************************* 56 全局的结构体声明 57 *********************************************************/ 58 59 typedef unsigned char uint8_t; 60 typedef unsigned short uint16_t; 61 typedef unsigned int uint32_t; 62 63 //考虑到内存对齐的问题 64 struct makeru_zigbee_info{ 65 uint8_t head[3]; //标识位: ‘m‘ ‘s‘ ‘m‘ makeru-security-monitor 66 uint8_t type; //数据类型 ‘z‘---zigbee ‘a‘---a9 67 float temperature; //温度 68 float humidity; //湿度 69 float tempMIN;//温度下限 70 float tempMAX;//温度上限 71 float humidityMIN; //湿度下限 72 float humidityMAX; //湿度上限 73 uint32_t reserved[2]; //保留扩展位,默认填充0 74 }; 75 76 struct makeru_a9_info{ 77 uint8_t head[3]; //标识位: ‘m‘ ‘s‘ ‘m‘ makeru-security-monitor 78 uint8_t type; //数据类型 ‘z‘---zigbee ‘a‘---a9 79 float adc; 80 short gyrox; //陀螺仪数据 81 short gyroy; 82 short gyroz; 83 short aacx; //加速计数据 84 short aacy; 85 short aacz; 86 uint32_t reserved[2]; //保留扩展位,默认填充0 87 }; 88 89 struct makeru_env_data{ 90 struct makeru_a9_info a9_info; 91 struct makeru_zigbee_info zigbee_info; 92 uint32_t reserved[2]; //保留扩展位,默认填充0 93 }; 94 95 //所有监控区域的信息结构体 96 struct env_info_client_addr 97 { 98 struct makeru_env_data monitor_no[MONITOR_NUM]; //数组 老家---新家 99 }; 100 101 102 103 104 105 /********************************************************* 106 全局的外部线程处理函数声明 107 *********************************************************/ 108 109 extern void *pthread_client_request (void *arg); //接收CGI 等的请求 110 extern void *pthread_refresh(void *arg); //刷新共享内存数据线程 111 extern void *pthread_sqlite(void *arg); //数据库线程,保存数据库的数据 112 extern void *pthread_transfer(void *arg); //接收ZigBee的数据并解析 113 extern void *pthread_sms(void *arg); //发送短信线程 114 extern void *pthread_buzzer(void *arg); //蜂鸣器控制线程 115 extern void *pthread_led(void *arg); //led灯控制线程 116 117 118 extern int send_msg_queue(long type,unsigned char text); 119 120 121 /********************************************************* 122 全局的消息队列传递的结构体声明 123 *********************************************************/ 124 125 //消息队列结构体 126 struct msg 127 { 128 long type;//从消息队列接收消息时用于判断的消息类型 129 long msgtype;//具体的消息类型 130 unsigned char text[QUEUE_MSG_LEN];//消息正文 131 }; 132 133 134 135 136 #endif
1 #include <stdio.h> 2 #include <pthread.h> 3 #include <errno.h> 4 #include <signal.h> 5 #include <unistd.h> 6 #include <sys/shm.h> 7 #include <sys/sem.h> 8 #include <sys/ipc.h> 9 #include "data_global.h" 10 11 12 void release_pthread_resource(int signo); 13 14 extern pthread_mutex_t mutex_client_request, 15 mutex_refresh, 16 mutex_sqlite, 17 mutex_transfer, 18 mutex_sms, 19 mutex_buzzer, 20 mutex_led; 21 22 extern pthread_cond_t cond_client_request, 23 cond_refresh, 24 cond_sqlite, 25 cond_transfer, 26 cond_transfer, 27 cond_sms, 28 cond_buzzer, 29 cond_led; 30 31 extern int msgid; 32 extern int shmid; 33 extern int semid; 34 35 pthread_t id_client_request, 36 id_refresh, 37 id_sqlite, 38 id_transfer, 39 id_sms, 40 id_buzzer, 41 id_led; 42 43 44 int main(int argc, const char *argv[]) 45 { 46 //初始化锁 47 pthread_mutex_init(&mutex_client_request,NULL); 48 pthread_mutex_init(&mutex_refresh,NULL); 49 pthread_mutex_init(&mutex_sqlite,NULL); 50 pthread_mutex_init(&mutex_transfer,NULL); 51 pthread_mutex_init(&mutex_sms,NULL); 52 pthread_mutex_init(&mutex_buzzer,NULL); 53 pthread_mutex_init(&mutex_led,NULL); 54 55 //等待接收信号,信号处理函数 56 //捕捉到Ctrl+C,释放线程资源 57 signal (SIGINT, release_pthread_resource); 58 59 //初始化各种条件变量 60 pthread_cond_init(&cond_client_request,NULL); 61 pthread_cond_init(&cond_refresh,NULL); 62 pthread_cond_init(&cond_sqlite,NULL); 63 pthread_cond_init(&cond_transfer,NULL); 64 pthread_cond_init(&cond_sms,NULL); 65 pthread_cond_init(&cond_buzzer,NULL); 66 pthread_cond_init(&cond_led,NULL); 67 68 //创建线程 69 //线程处理函数在"data_global.h"中声明 70 pthread_create(&id_client_request, NULL,pthread_client_request,NULL); 71 pthread_create(&id_refresh, NULL,pthread_refresh,NULL); 72 pthread_create(&id_sqlite, NULL,pthread_sqlite,NULL); 73 pthread_create(&id_transfer, NULL,pthread_transfer,NULL); 74 pthread_create(&id_sms, NULL,pthread_sms,NULL); 75 pthread_create(&id_buzzer, NULL,pthread_buzzer,NULL); 76 pthread_create(&id_led, NULL,pthread_led,NULL); 77 78 //以阻塞的方式等待thread指定的线程结束 79 // 80 //当函数返回时,被等待线程的资源被收回 81 pthread_join(id_client_request,NULL); printf ("pthread1 "); 82 pthread_join(id_refresh,NULL); printf ("pthread2 "); 83 pthread_join(id_sqlite,NULL); printf ("pthread3 "); 84 pthread_join(id_transfer,NULL); printf ("pthread4 "); 85 pthread_join(id_sms,NULL); printf ("pthread5 "); 86 pthread_join(id_buzzer,NULL); printf ("pthread6 "); 87 pthread_join(id_led,NULL); printf ("pthread7 "); 88 89 return 0; 90 } 91 92 //释放锁和条件变量等线程资源(不会自己释放,需要手动释放) 93 void release_pthread_resource(int signo) 94 { 95 96 pthread_mutex_destroy (&mutex_client_request); 97 pthread_mutex_destroy (&mutex_refresh); 98 pthread_mutex_destroy (&mutex_sqlite); 99 pthread_mutex_destroy (&mutex_transfer); 100 pthread_mutex_destroy (&mutex_sms); 101 pthread_mutex_destroy (&mutex_buzzer); 102 pthread_mutex_destroy (&mutex_led); 103 104 pthread_cond_destroy (&cond_client_request); 105 pthread_cond_destroy (&cond_refresh); 106 pthread_cond_destroy (&cond_sqlite); 107 pthread_cond_destroy (&cond_transfer); 108 pthread_cond_destroy (&cond_sms); 109 pthread_cond_destroy (&cond_buzzer); 110 pthread_cond_destroy (&cond_led); 111 112 //分离线程资源,主线程与子线程分离,子线程结束后,资源自动回收。 113 pthread_detach(id_client_request); 114 pthread_detach(id_refresh); 115 pthread_detach(id_sqlite); 116 pthread_detach(id_transfer); 117 pthread_detach(id_sms); 118 pthread_detach(id_buzzer); 119 pthread_detach(id_led); 120 121 printf("all pthread is detached "); 122 123 //释放内存中映射的资源 124 msgctl (msgid, IPC_RMID, NULL); 125 shmctl (shmid, IPC_RMID, NULL); 126 semctl (semid, 1, IPC_RMID, NULL); 127 128 exit(0); 129 }
1 #include "data_global.h" 2 3 4 //接收ZigBee的数据和采集的A9平台的传感器数据 5 6 void *pthread_transfer(void *arg) 7 { 8 9 printf("pthread_analysis "); 10 11 12 13 }
1 #include "data_global.h" 2 3 //:数据库线程. 4 void *pthread_sqlite(void *arg) 5 { 6 7 printf("pthread_sqlite "); 8 }
1 #include "data_global.h" 2 3 void *pthread_sms(void *arg) 4 { 5 6 printf("pthread_sms "); 7 }
1 #include "data_global.h" 2 #include "sem.h" 3 4 #define N 1024 //for share memory 5 6 extern int shmid; 7 extern int msgid; 8 extern int semid; 9 10 extern key_t shm_key; 11 extern key_t sem_key; 12 extern key_t key; //msg_key 13 14 extern pthread_mutex_t mutex_client_request, 15 mutex_refresh, 16 mutex_sqlite, 17 mutex_transfer, 18 mutex_analysis, 19 mutex_sms, 20 mutex_buzzer, 21 mutex_led, 22 mutex_camera; 23 24 extern pthread_cond_t cond_client_request, 25 cond_refresh, 26 cond_sqlite, 27 cond_transfer, 28 cond_analysis, 29 cond_sms, 30 cond_buzzer, 31 cond_led, 32 cond_camera; 33 34 35 extern struct env_info_client_addr sm_all_env_info; 36 37 struct shm_addr 38 { 39 char shm_status; //shm_status可以等于home_id,用来区分共享内存数据 40 struct env_info_client_addr sm_all_env_info; 41 }; 42 struct shm_addr *shm_buf; 43 44 int file_env_info_struct(struct env_info_client_addr *rt_status,int home_id); 45 46 void *pthread_refresh(void *arg) 47 { 48 //semaphore for access to resource limits 49 if((sem_key = ftok("/tmp",‘i‘)) < 0){ 50 perror("ftok failed . "); 51 exit(-1); 52 } 53 54 semid = semget(sem_key,1,IPC_CREAT|IPC_EXCL|0666); 55 if(semid == -1) { 56 if(errno == EEXIST){ 57 semid = semget(sem_key,1,0777); 58 }else{ 59 perror("fail to semget"); 60 exit(1); 61 } 62 }else{ 63 init_sem (semid, 0, 1); 64 } 65 66 //share memory for env_info refresh config 67 if((shm_key = ftok("/tmp",‘i‘)) < 0){ 68 perror("ftok failed . "); 69 exit(-1); 70 } 71 72 shmid = shmget(shm_key,N,IPC_CREAT|IPC_EXCL|0666); 73 if(shmid == -1) { 74 if(errno == EEXIST){ 75 shmid = shmget(key,N,0777); 76 }else{ 77 perror("fail to shmget"); 78 exit(1); 79 } 80 } 81 82 //share memap 83 if((shm_buf = (struct shm_addr *)shmat(shmid,NULL,0)) == (void *)-1) 84 { 85 perror("fail to shmat"); 86 exit(1); 87 } 88 89 printf("pthread_refresh ......>>>>>>> "); 90 91 #if 0 92 bzero (shm_buf, sizeof (struct shm_addr)); 93 while(1){ 94 sem_p(semid,0); 95 shm_buf->shm_status = 1; 96 file_env_info_struct(&shm_buf->sm_all_env_info,shm_buf->shm_status); 97 sleep(1); 98 sem_v(semid,0); 99 } 100 #endif 101 102 103 } 104 105 106 107 int file_env_info_struct(struct env_info_client_addr *rt_status,int home_id) 108 { 109 int env_info_size = sizeof(struct env_info_client_addr); 110 printf("env_info_size = %d. ",env_info_size); 111 112 rt_status->monitor_no[home_id].zigbee_info.temperature = 10.0; 113 rt_status->monitor_no[home_id].zigbee_info.tempMIN = 2.0; 114 rt_status->monitor_no[home_id].zigbee_info.tempMAX = 20.0; 115 rt_status->monitor_no[home_id].zigbee_info.humidity = 20.0; 116 rt_status->monitor_no[home_id].zigbee_info.humidityMIN = 10.0; 117 rt_status->monitor_no[home_id].zigbee_info.humidityMAX = 30.0; 118 rt_status->monitor_no[home_id].zigbee_info.reserved[0] = 0.01; 119 rt_status->monitor_no[home_id].zigbee_info.reserved[1] = -0.01; 120 121 122 rt_status->monitor_no[home_id].a9_info.adc = 9.0; 123 rt_status->monitor_no[home_id].a9_info.gyrox = -14.0; 124 rt_status->monitor_no[home_id].a9_info.gyroy = 20.0; 125 rt_status->monitor_no[home_id].a9_info.gyroz = 40.0; 126 rt_status->monitor_no[home_id].a9_info.aacx = 642.0; 127 rt_status->monitor_no[home_id].a9_info.aacy = -34.0; 128 rt_status->monitor_no[home_id].a9_info.aacz = 5002.0; 129 rt_status->monitor_no[home_id].a9_info.reserved[0] = 0.01; 130 rt_status->monitor_no[home_id].a9_info.reserved[1] = -0.01; 131 132 return 0; 133 }
1 #include "data_global.h" 2 3 4 //:A9LED模块线程. 5 void *pthread_led(void *arg) 6 { 7 printf("pthread_led "); 8 #if 0 9 5. open(dev_led, ) 10 6. pthread_cond_wait (cond_led, ); 11 7. 获取dev_led_mask(控制标志) 12 8. 通过ioctl()控制led 13 #endif 14 }
1 #include "data_global.h" 2 3 extern int msgid; 4 extern key_t key; 5 6 extern pthread_mutex_t mutex_client_request, 7 mutex_refresh, 8 mutex_sqlite, 9 mutex_transfer, 10 mutex_analysis, 11 mutex_sms, 12 mutex_buzzer, 13 mutex_led, 14 mutex_camera; 15 16 extern pthread_cond_t cond_client_request, 17 cond_refresh, 18 cond_sqlite, 19 cond_transfer, 20 cond_analysis, 21 cond_sms, 22 cond_buzzer, 23 cond_led, 24 cond_camera; 25 26 extern char recive_phone[12] ; 27 extern char center_phone[12] ; 28 29 30 struct msg msgbuf; 31 32 33 34 35 //:处理消息队列里请求的线程. 36 void *pthread_client_request(void *arg) 37 { 38 if((key = ftok("/tmp",‘g‘)) < 0){ 39 perror("ftok failed . "); 40 exit(-1); 41 } 42 43 msgid = msgget(key,IPC_CREAT|IPC_EXCL|0666); 44 if(msgid == -1) { 45 if(errno == EEXIST){ 46 msgid = msgget(key,0777); 47 }else{ 48 perror("fail to msgget"); 49 exit(1); 50 } 51 } 52 printf("pthread_client_request "); 53 54 while(1){ 55 bzero(&msgbuf,sizeof(msgbuf)); 56 printf("wait form client request... "); 57 msgrcv (msgid, &msgbuf, sizeof (msgbuf) - sizeof (long), 1L, 0); 58 printf ("Get %ldL msg ", msgbuf.msgtype); 59 printf ("text[0] = %#x ", msgbuf.text[0]); 60 61 switch(msgbuf.msgtype){ 62 case 1L: 63 printf("hello led "); 64 break; 65 case 2L: 66 printf("hello beep "); 67 break; 68 case 3L: 69 printf("hello seg "); 70 break; 71 case 4L: 72 printf("hello fan "); 73 break; 74 75 case 5L: 76 printf("set env data "); 77 printf("temMAX: %d ",*((int *)&msgbuf.text[1])); 78 printf("temMIN: %d ",*((int *)&msgbuf.text[5])); 79 printf("humMAX: %d ",*((int *)&msgbuf.text[9])); 80 printf("humMAX: %d ",*((int *)&msgbuf.text[13])); 81 printf("illMAX: %d ",*((int *)&msgbuf.text[17])); 82 printf("illMAX: %d ",*((int *)&msgbuf.text[21])); 83 84 break; 85 case 6L: 86 case 7L: 87 case 8L: 88 case 9L: 89 printf("?¿??«?Щ??À©?À´??£¬¼?? "); 90 break; 91 case 10L: 92 { 93 int i = 0 , j = 0 ; 94 for(i = 0 ; i < 11; i++){ 95 recive_phone[i] = msgbuf.text[i]; 96 } 97 recive_phone[i] = ‘