aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 82c5d0d3c17bf07c3fa5d75cc817ad217a3bba6e (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * File Name:       main.c
 * Workflow:        prepare serial port, create MQ, SHM, child threads,
 *                  register SIGINT to shutdown demo, recycle IPC waste.
 * Return Value:    0, normal exit. -1, erroneous exit.
 */

/* Header files */
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "debug.h"
#include "proto.h"
#include "uart_api.h"

/* external threads */
extern void *pthread_gprs(void *args);
extern void *pthread_fan(void *args);
extern void *pthread_dht11(void *args);

/* global variables*/
pthread_t id_dht11, id_fan, id_gprs;

pthread_mutex_t mutex_zigbee_coordinator, mutex_gprs;

volatile sig_atomic_t should_exit = 0;

/* functions */
static void cleanThreadsHandler(int signum);

/* main */
int main(void) {
  int *p_coordinator_fd = malloc(sizeof(int));
  int retVal;
  int *p_gprs_fd = malloc(sizeof(int));
  key_t *p_shm_key = malloc(sizeof(key_t));
  key_t *p_msg_key = malloc(sizeof(key_t));
  int *p_shm_id = malloc(sizeof(int));
  int *p_msg_id = malloc(sizeof(int));
  ARG_STRUCT *p_dht11_resource_struct = malloc(sizeof(ARG_STRUCT));
  ARG_STRUCT *p_fan_resource_struct = malloc(sizeof(ARG_STRUCT));
  ARG_STRUCT *p_gprs_resource_struct = malloc(sizeof(ARG_STRUCT));

  // prepare serial communication
  if ((*p_coordinator_fd = open_port("/dev/ttyUSB0")) == -1) {
    fprintf(stderr, "[%s:%d] open_port: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Get zigbee coordinator fd: %d.\n", __FUNCTION__,
           __LINE__, *p_coordinator_fd);

  if ((retVal = set_comfig(*p_coordinator_fd, 115200)) != 0) {
    fprintf(stderr, "[%s:%d] set_comfig: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Set Zigbee coordinator serial config with "
           "return value: %d.\n",
           __FUNCTION__, __LINE__, retVal);

  if ((*p_gprs_fd = open_port("/dev/ttyUSB1")) == -1) {
    fprintf(stderr, "[%s:%d] open_port: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Get GPRS fd : %d.\n", __FUNCTION__, __LINE__,
           *p_gprs_fd);

  if ((retVal = set_comfig(*p_gprs_fd, 9600)) != 0) {
    fprintf(stderr, "[%s:%d] set_comfig: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog(
      "[DEBUG-INFO %s:%d]: Set GPRS serial config with return value: %d.\n",
      __FUNCTION__, __LINE__, retVal);

  // prepare IPC
  if ((*p_shm_key = ftok("/usr/bin/boa", 'm')) == -1) {
    fprintf(stderr, "[%s:%d] ftok: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Generate shared memory key : %d.\n",
           __FUNCTION__, __LINE__, *p_shm_key);

  if ((*p_msg_key = ftok("/usr/bin/boa", 'q')) == -1) {
    fprintf(stderr, "[%s:%d] ftok: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Generate message queue key: %d.\n",
           __FUNCTION__, __LINE__, *p_msg_key);

  if ((*p_shm_id = shmget(*p_shm_key, sizeof(SHM_STRUCT),
                          IPC_CREAT | IPC_EXCL | 0600)) == -1) {
    fprintf(stderr, "[%s:%d] shmget: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Generate shared memory id: %d.\n", __FUNCTION__,
           __LINE__, *p_shm_id);

  if ((*p_msg_id = msgget(*p_msg_key, IPC_CREAT | IPC_EXCL | 0600)) == -1) {
    fprintf(stderr, "[%s:%d] msgget: %s", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Generate message queue id: %d.\n", __FUNCTION__,
           __LINE__, *p_msg_id);

  // register SIGINT to cleanThreadsHandler function
  struct sigaction sa_struct;
  sa_struct.sa_handler = cleanThreadsHandler;
  sa_struct.sa_flags = 0;

  // except SIGINT all signals are blocked
  sigfillset(&sa_struct.sa_mask);
  sigdelset(&sa_struct.sa_mask, SIGINT);

  if ((retVal = sigaction(SIGINT, &sa_struct, NULL)) == -1) {
    fprintf(stderr, "[%s:%d] sigaction: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: sigaction with return value: %d.\n",
           __FUNCTION__, __LINE__, retVal);

  // prepare mutex
  if ((retVal = pthread_mutex_init(&mutex_zigbee_coordinator, NULL)) != 0) {
    fprintf(stderr, "[%s:%d] pthread_mutex_init: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }

  if ((retVal = pthread_mutex_init(&mutex_gprs, NULL)) != 0) {
    fprintf(stderr, "[%s:%d] pthread_mutex_init: %s.\n", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }

  // create child threads
  memset(p_dht11_resource_struct, '\0', sizeof(ARG_STRUCT));

  p_dht11_resource_struct->rw_fd = *p_coordinator_fd;
  DebugLog(
      "[DEBUG-INFO %s:%d]: Pass to child thread dht11 read/write fd: %d.\n",
      __FUNCTION__, __LINE__, p_dht11_resource_struct->rw_fd);

  p_dht11_resource_struct->shm_id = *p_shm_id;
  DebugLog(
      "[DEBUG-INFO %s:%d]: Pass to child thread dht11 shared memory id: %d.\n",
      __FUNCTION__, __LINE__, p_dht11_resource_struct->shm_id);

  if ((retVal = pthread_create(&id_dht11, NULL, pthread_dht11,
                               p_dht11_resource_struct)) != 0) {
    fprintf(stderr, "[%s:%d] pthread_create: %s", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog("[DEBUG-INFO %s:%d]: Create child thread pthread_dht11 with return "
           "value: %d.\n",
           __FUNCTION__, __LINE__, retVal);

  memset(p_fan_resource_struct, '\0', sizeof(ARG_STRUCT));

  p_fan_resource_struct->rw_fd = *p_coordinator_fd;
  DebugLog(
      "[DEBUG-INFO %s:%d]: Pass to child thread pthread_fan read/write fd: "
      "%d.\n",
      __FUNCTION__, __LINE__, p_fan_resource_struct->rw_fd);

  p_fan_resource_struct->mq_id = *p_msg_id;
  DebugLog(
      "[DEBUG-INFO %s:%d]: Pass to child thread pthread_fan message queue id: "
      "%d.\n",
      __FUNCTION__, __LINE__, p_fan_resource_struct->mq_id);

  if ((retVal = pthread_create(&id_fan, NULL, pthread_fan,
                               p_fan_resource_struct)) != 0) {
    fprintf(stderr, "[%s:%d] pthread_create: %s", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog(
      "[DEBUG-INFO %s:%d]: Create child thread pthread_fan with return value: "
      "%d\n",
      __FUNCTION__, __LINE__, retVal);

  memset(p_gprs_resource_struct, '\0', sizeof(ARG_STRUCT));

  p_gprs_resource_struct->rw_fd = *p_gprs_fd;
  DebugLog("[DEBUG-INFO %s:%d]: Pass to child thread gprs read/write fd: %d.\n",
           __FUNCTION__, __LINE__, p_gprs_resource_struct->rw_fd);

  p_gprs_resource_struct->mq_id = *p_msg_id;
  DebugLog(
      "[DEBUG-INFO %s:%d]: Pass to child thread gprs message queue id: %d.\n",
      __FUNCTION__, __LINE__, p_gprs_resource_struct->mq_id);

  if ((retVal = pthread_create(&id_gprs, NULL, pthread_gprs,
                               p_gprs_resource_struct)) != 0) {
    fprintf(stderr, "[%s:%d] pthread_create: %s", __FUNCTION__, __LINE__,
            strerror(errno));
    exit(-1);
  }
  DebugLog(
      "[DEBUG-INFO %s:%d]: Create child thread pthread_gprs with return value: "
      "%d.\n",
      __FUNCTION__, __LINE__, retVal);

  // wait for child thread return
  pthread_join(id_dht11, NULL);
  pthread_join(id_fan, NULL);
  pthread_join(id_gprs, NULL);

  // destory mutex
  pthread_mutex_destroy(&mutex_zigbee_coordinator);
  pthread_mutex_destroy(&mutex_gprs);

  // remove IPC
  shmctl(*p_shm_id, IPC_RMID, NULL);
  msgctl(*p_msg_id, IPC_RMID, NULL);

  // close file describer
  close(*p_coordinator_fd);
  close(*p_gprs_fd);

  // free memory
  free(p_coordinator_fd);
  free(p_gprs_fd);
  free(p_shm_key);
  free(p_msg_key);
  free(p_shm_id);
  free(p_msg_id);
  free(p_dht11_resource_struct);
  free(p_fan_resource_struct);
  free(p_gprs_resource_struct);

  return 0;
}

static void cleanThreadsHandler(int signum) {
  if (signum == SIGINT) {
    should_exit = 1;
  }
}