blob: f510adf8b8b9303ce5985144c12100553759243d
1 | #include <CTvLog.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <unistd.h> |
5 | #include <sys/prctl.h> |
6 | #include <fcntl.h> |
7 | #include <errno.h> |
8 | |
9 | #include <cutils/log.h> |
10 | |
11 | #include "CSerialCommunication.h" |
12 | |
13 | #ifdef LOG_TAG |
14 | #undef LOG_TAG |
15 | #define LOG_TAG "CSerialCommunication" |
16 | #endif |
17 | |
18 | CSerialCommunication::CSerialCommunication() |
19 | { |
20 | int i = 0; |
21 | |
22 | mpObserver = NULL; |
23 | mState = STATE_STOPED; |
24 | |
25 | mFileName[0] = 0; |
26 | for (i = 0; i < CC_MAX_SERIAL_RD_BUF_LEN; i++) { |
27 | mDataBuf[i] = 0; |
28 | } |
29 | } |
30 | |
31 | CSerialCommunication::~CSerialCommunication() |
32 | { |
33 | mSerialPort.CloseDevice(); |
34 | } |
35 | |
36 | int CSerialCommunication::OpenModule(int serial_dev_id) |
37 | { |
38 | int tmp_ret = 0; |
39 | |
40 | if (mSerialPort.OpenDevice(serial_dev_id) < 0) { |
41 | return -1; |
42 | } |
43 | |
44 | return mSerialPort.setup_serial(); |
45 | } |
46 | |
47 | int CSerialCommunication::CloseModule() |
48 | { |
49 | stop(); |
50 | mSerialPort.CloseDevice(); |
51 | return 0; |
52 | } |
53 | |
54 | int CSerialCommunication::start() |
55 | { |
56 | if (mState == STATE_STOPED) { |
57 | this->run(); |
58 | } |
59 | |
60 | return 0; |
61 | } |
62 | |
63 | int CSerialCommunication::stop() |
64 | { |
65 | requestExit(); |
66 | mState = STATE_STOPED; |
67 | |
68 | return 0; |
69 | } |
70 | |
71 | int CSerialCommunication::sendData(int data_len, unsigned char data_buf[]) |
72 | { |
73 | mSerialPort.writeFile(data_buf, data_len); |
74 | return 0; |
75 | } |
76 | |
77 | bool CSerialCommunication::threadLoop() |
78 | { |
79 | int rd_len = 0; |
80 | int thread_dly_tm = 100 * 1000; |
81 | |
82 | if (mpObserver == NULL) { |
83 | return false; |
84 | } |
85 | |
86 | if (mSerialPort.getFd() < 0) { |
87 | return false; |
88 | } |
89 | |
90 | if (mSerialPort.getDevId() < 0) { |
91 | return false; |
92 | } |
93 | |
94 | LOGD("%s, entering...\n", "TV"); |
95 | |
96 | prctl(PR_SET_NAME, (unsigned long)"CSerialCommunication thread loop"); |
97 | |
98 | mState = STATE_RUNNING; |
99 | |
100 | while (!exitPending()) { //requietexit() or requietexitWait() not call |
101 | memset(mDataBuf, 0, CC_MAX_SERIAL_RD_BUF_LEN); |
102 | rd_len = mSerialPort.readFile(mDataBuf, CC_MAX_SERIAL_RD_BUF_LEN); |
103 | if (rd_len <= 0) { |
104 | usleep(thread_dly_tm); |
105 | continue; |
106 | } |
107 | |
108 | LOGD("%s, rd_len = %d.\n", __FUNCTION__, rd_len); |
109 | |
110 | if (rd_len > CC_MAX_SERIAL_RD_BUF_LEN) { |
111 | rd_len = CC_MAX_SERIAL_RD_BUF_LEN; |
112 | } |
113 | |
114 | LOGD("%s, send data to java.", __FUNCTION__); |
115 | //send data to java |
116 | mpObserver->onSerialCommunication(mSerialPort.getDevId(), rd_len, mDataBuf); |
117 | |
118 | usleep(thread_dly_tm); |
119 | } |
120 | |
121 | mState = STATE_STOPED; |
122 | |
123 | LOGD("%s, exiting...\n", "TV"); |
124 | //return true, run again, return false,not run. |
125 | return false; |
126 | } |
127 |