RDK Documentation (Open Sourced RDK Components)
Connection.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22 * @defgroup trm
23 * @{
24 * @defgroup qtapp
25 * @{
26 **/
27 
28 
29 #ifndef _TRM_CONNECTION_H
30 #define _TRM_CONNECTION_H
31 
32 #include <QTcpSocket>
33 #include <QObject>
34 #include <QDebug>
35 #include <QQueue>
36 #include <QByteArray>
37 
38 #include "trm/TRM.h"
39 #include "trm/Header.h"
40 
41 namespace TRM {
42 
43 class Connection : public QObject
44 {
45  Q_OBJECT
46 public:
47 
48  static const uint32_t kInvalidClientId = (~0);
49  static const uint32_t kRecorderClientId = 0XFFFFFF00;
50  static const uint32_t kTunerAgentId = 0XFFFFFF02;
51  static const uint32_t kSDVAgentId = 0XFFFFFF03;
52  static const uint32_t kTRMMgrClientId = 0XFFFFFF04;
53  static const uint32_t kTrmClientId = 0x00000000;
54  static const uint32_t kTrmMonitorId = 0xFFFFFFF0;
55  static bool isTestClient(uint32_t clientId) {
56  return ((clientId != kRecorderClientId) &&
57  ((clientId >= kTrmMonitorId)));
58  }
59 
60  static bool isXREClient(uint32_t clientId) {
61  return (clientId > 0 && clientId < 0xFFFFFF00);
62  }
63 
64  static bool isTunerAgentClient(uint32_t clientId) {
65  return ((clientId == kTunerAgentId));
66  }
67 
68  static bool isTRMDiagClient(uint32_t clientId) {
69  return ((clientId == kTRMMgrClientId));
70  }
71 
72  static bool isSDVAgentClient(uint32_t clientId) {
73  return ((clientId == kSDVAgentId));
74  }
75 
76  Connection(QTcpSocket &_tcpSocket);
77  virtual ~Connection();
78 
79  void send(const std::vector<uint8_t> &out) const;
80  void sendMessage(const QByteArray &outgoingMessage) const;
81  /* caller of recvMessage(void) must free the buffer received */
82  void recvMessage(QByteArray &recvBuffer) const;
83  const QByteArray & recvMessage(void) const;
84 
85  void sendAsync(const std::vector<uint8_t> &out) const;
86 
87 private:
88  class State {
89  public:
90  State(void) : dataLength(0) {}
91  virtual QByteArray & read(QAbstractSocket &socket, QByteArray &readBuffer) = 0;
92  virtual const char * stateName(void) = 0;
93  void setDataLength(size_t length) { dataLength = length;};
94  size_t getDataLength(void) { return dataLength;};
95  private:
96  size_t dataLength;
97  };
98 
99  class IdleState : public State {
100  public:
101  QByteArray & read(QAbstractSocket &socket, QByteArray &readBuffer);
102  const char * stateName(void) { return "IdleState";};
103  };
104 
105  class WaitHeaderState : public State {
106  public:
107  QByteArray & read(QAbstractSocket &socket, QByteArray &readBuffer);
108  const char * stateName(void) { return "WaitHeaderState";};
109  };
110 
111  class WaitPayloadState : public State {
112  public:
113  QByteArray & read(QAbstractSocket &socket, QByteArray &readBuffer);
114  const char * stateName(void) { return "WaitPayloadState";};
115  };
116 
117  QAbstractSocket & getSocket(void)
118  {
119  return socket;
120  }
121 
122 
123 signals:
124  void disconnected(Connection *);
125  void messageReceived(const Connection &);
126  void readyRead(void);
127  void hasMessageToSend(const std::vector<uint8_t> out) const;
128 
129 public slots:
130  void onConnected();
131  void onReadyRead(void);
132  void onBytesWritten(qint64 bytes);
133  void onDisconnected(void);
134  void onSocketError(QAbstractSocket::SocketError socketError);
135  void onStateChanged(QAbstractSocket::SocketState socketState);
136  void onHasMessageToSend(const std::vector<uint8_t> out) const;
137 
138 private:
139  IdleState idleState;
140  WaitHeaderState waitHeaderState;
141  WaitPayloadState waitPayloadState;
142 
143  QAbstractSocket &socket;
144  mutable QQueue<QByteArray *> incomingMessages;
145  QByteArray *messageByteArray;
146  State *state;
147  mutable uint32_t clientId;
148 };
149 
150 }
151 #endif
152 
153 
154 /** @} */
155 /** @} */
Header.h
TRM::Connection::IdleState
Definition: Connection.h:99
TRM::Connection::WaitPayloadState
Definition: Connection.h:111
TRM::Connection::State
Definition: Connection.h:88
TRM::Connection::WaitHeaderState
Definition: Connection.h:105
TRM::Connection
Definition: Connection.h:43