aboutsummaryrefslogtreecommitdiffstats
path: root/src/pthread_gprs.c
blob: 5d1994e71503d56526efff764f0819d3a6f00699 (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
/*
 * File Name:       pthread_gprs.c
 * Workflow:        continually listen to MQ, write AT commands
 * Note:			AT commands must endline with '\n'
 * 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_gprs;
extern volatile sig_atomic_t should_exit;

void *pthread_gprs(void *args) {
  ARG_STRUCT *p_resource_struct;
  MQ_STRUCT *p_mq_struct = NULL;
  ssize_t rwRetCount;
  char staging_telnum[12] = "18851792375";
  char rw_tempbuf[32] = {'\0'};

  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);

  // read from message queue
  while (!should_exit) {
    msgrcv(p_resource_struct->mq_id, p_mq_struct,
           sizeof(MQ_STRUCT) - sizeof(long), 21L, 0);
    memset(rw_tempbuf, '\0', sizeof(rw_tempbuf));
    pthread_mutex_lock(&mutex_gprs);
    if (p_mq_struct->tswitch == 1) {
      // init module
      strcpy(rw_tempbuf, "AT;\n");
      rwRetCount =
          write(p_resource_struct->rw_fd, rw_tempbuf, strlen(rw_tempbuf));
      if (rwRetCount < 4) {
        fprintf(stderr, "[%s:%d] Not enough bytes written.\n", __FUNCTION__,
                __LINE__);
        continue;
      }
      DebugLog("[DEBUG-INFO %s:%d]: Write AT command, %zd Byte(s) written.\n",
               __FUNCTION__, __LINE__, rwRetCount);

      // alarm call
      memset(rw_tempbuf, '\0', sizeof(rw_tempbuf));
      sprintf(rw_tempbuf, "ATD%s;", staging_telnum);
      strcat(rw_tempbuf, "\n");
      rwRetCount =
          write(p_resource_struct->rw_fd, rw_tempbuf, strlen(rw_tempbuf));
      if (rwRetCount < 16) {
        fprintf(stderr, "[%s:%d] Not enough bytes written.\n", __FUNCTION__,
                __LINE__);
        continue;
      }
      DebugLog("[DEBUG-INFO %s:%d]: Write AT command, %zd Byte(s) written.\n",
               __FUNCTION__, __LINE__, rwRetCount);
    } else if (p_mq_struct->tswitch == 0) {
      strcpy(rw_tempbuf, "ATH;\n");
      rwRetCount =
          write(p_resource_struct->rw_fd, rw_tempbuf, strlen(rw_tempbuf));
      if (rwRetCount < 5) {
        fprintf(stderr, "[%s:%d] Not enough bytes written.\n", __FUNCTION__,
                __LINE__);
        continue;
      }
      DebugLog("[DEBUG-INFO %s:%d]: Write AT command, %zd Byte(s) written.\n",
               __FUNCTION__, __LINE__, rwRetCount);
    }
    pthread_mutex_unlock(&mutex_gprs);
  }
  pthread_exit(NULL);
}