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