1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/*
* File Name: pthread_fan.c
* Workflow: continually listen to MQ, write ON/OFF
* Return Value: NULL always
*/
/* Header files */
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include "debug.h"
#include "proto.h"
/* external variables */
extern pthread_mutex_t mutex_zigbee_coordinator;
extern volatile sig_atomic_t should_exit;
void *pthread_fan(void *args) {
ARG_STRUCT *p_resource_struct = malloc(sizeof(ARG_STRUCT));
MQ_STRUCT *p_mq_struct = malloc(sizeof(MQ_STRUCT));
ssize_t rwRetCount;
p_resource_struct = args;
memset(p_mq_struct, '\0', sizeof(MQ_STRUCT));
DebugLog("[DEBUG-INFO %s:%d]: Get read/write file describer: %d.\n",
__FUNCTION__, __LINE__, p_resource_struct->rw_fd);
DebugLog("[DEBUG-INFO %s:%d]: Get message queue ID: %d.\n", __FUNCTION__,
__LINE__, p_resource_struct->mq_id);
while (!should_exit) {
/* read from message queue */
msgrcv(p_resource_struct->mq_id, p_mq_struct,
sizeof(MQ_STRUCT) - sizeof(long), 20L, 0);
pthread_mutex_lock(&mutex_zigbee_coordinator);
if (p_mq_struct->tswitch == 0) {
rwRetCount = write(p_resource_struct->rw_fd, "OFF", sizeof("OFF"));
if (rwRetCount < 3) {
fprintf(stderr, "[%s:%d] Not enough bytes written.\n", __FUNCTION__,
__LINE__);
continue;
}
DebugLog(
"[DEBUG-INFO %s:%d]: Write Zigbee command, %zd Byte(s) written.\n",
__FUNCTION__, __LINE__, rwRetCount);
} else if (p_mq_struct->tswitch == 1) {
rwRetCount = write(p_resource_struct->rw_fd, "ON", sizeof("ON"));
if (rwRetCount < 2) {
fprintf(stderr, "[%s:%d] Not enough bytes written.\n", __FUNCTION__,
__LINE__);
continue;
}
DebugLog(
"[DEBUG-INFO %s:%d]: Write Zigbee command, %zd Byte(s) written.\n",
__FUNCTION__, __LINE__, rwRetCount);
}
pthread_mutex_unlock(&mutex_zigbee_coordinator);
}
free(p_resource_struct);
free(p_mq_struct);
pthread_exit(NULL);
}
|