mqttclient.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <errno.h>
  2. #include <unistd.h>
  3. #include "mqttclient.h"
  4. using namespace mosqpp;
  5. /////////////////////////////////////////////////////////////////////////////
  6. //
  7. template <typename T>
  8. static T _min(T v1, T v2)
  9. {
  10. return v1 < v2 ? v1 : v2;
  11. }
  12. /////////////////////////////////////////////////////////////////////////////
  13. //
  14. #define MQTTCL_DEFAULT_LOOP_TIMEOUT 30
  15. /////////////////////////////////////////////////////////////////////////////
  16. //
  17. CMqttClient::CMqttClient(const char *id) : mosquittopp(id),
  18. m_pEvtCallback(NULL),
  19. m_evtMask(0),
  20. m_pUserParam(NULL)
  21. {
  22. }
  23. CMqttClient::~CMqttClient(void)
  24. {
  25. }
  26. /////////////////////////////////////////////////////////////////////////////
  27. //
  28. int CMqttClient::Init(void)
  29. {
  30. return lib_init();
  31. }
  32. int CMqttClient::Cleanup(void)
  33. {
  34. return lib_cleanup();
  35. }
  36. /////////////////////////////////////////////////////////////////////////////
  37. //
  38. int CMqttClient::publish(CMqttMessage *pMsg)
  39. {
  40. return mosquittopp::publish(&pMsg->mid, pMsg->topic, pMsg->payloadlen, pMsg->payload, pMsg->qos, pMsg->retain);
  41. }
  42. /////////////////////////////////////////////////////////////////////////////
  43. //
  44. CMqttMessage* CMqttClient::PopRcvMsg(void)
  45. {
  46. return m_rcvMsg.Pop();
  47. }
  48. void CMqttClient::PushRcvMsg(CMqttMessage *pMsg)
  49. {
  50. m_rcvMsg.Push(pMsg);
  51. }
  52. CMqttMessage* CMqttClient::PopSndMsg(void)
  53. {
  54. return m_sndMsg.Pop();
  55. }
  56. void CMqttClient::PushSndMsg(CMqttMessage *pMsg)
  57. {
  58. m_sndMsg.Push(pMsg);
  59. }
  60. int CMqttClient::timed_loop(pc_time64_t nLoopTime)
  61. {
  62. int nRet, nLoopTimeout;
  63. pc_time64_t nRemaining = nLoopTime;
  64. m_pc.ClockTrigger();
  65. do
  66. {
  67. nLoopTimeout = _min(MQTTCL_DEFAULT_LOOP_TIMEOUT, (int)(nRemaining / _PC_NS_PER_MS));
  68. if((nRet = loop(nLoopTimeout)) != MOSQ_ERR_SUCCESS)
  69. break;
  70. if((nRemaining = nLoopTime - m_pc.ClockGetElapsed()) > (10 * _PC_NS_PER_MS))
  71. {
  72. m_pc.MSleep(10);
  73. nRemaining = nLoopTime - m_pc.ClockGetElapsed();
  74. }
  75. }
  76. while(nRemaining > (MQTTCL_DEFAULT_LOOP_TIMEOUT * _PC_NS_PER_MS));
  77. return nRet;
  78. }
  79. /*
  80. MOSQ_ERR_CONN_PENDING = -1,
  81. MOSQ_ERR_SUCCESS = 0,
  82. MOSQ_ERR_NOMEM = 1,
  83. MOSQ_ERR_PROTOCOL = 2,
  84. MOSQ_ERR_INVAL = 3,
  85. MOSQ_ERR_NO_CONN = 4,
  86. MOSQ_ERR_CONN_REFUSED = 5,
  87. MOSQ_ERR_NOT_FOUND = 6,
  88. MOSQ_ERR_CONN_LOST = 7,
  89. MOSQ_ERR_TLS = 8,
  90. MOSQ_ERR_PAYLOAD_SIZE = 9,
  91. MOSQ_ERR_NOT_SUPPORTED = 10,
  92. MOSQ_ERR_AUTH = 11,
  93. MOSQ_ERR_ACL_DENIED = 12,
  94. MOSQ_ERR_UNKNOWN = 13,
  95. MOSQ_ERR_ERRNO = 14,
  96. MOSQ_ERR_EAI = 15,
  97. MOSQ_ERR_PROXY = 16
  98. */
  99. bool CMqttClient::EvalError(int nErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr)
  100. {
  101. bool bRet;
  102. rbReconnect = rbConnPending = false;
  103. if(rbIntr)
  104. return false;
  105. switch(nErr)
  106. {
  107. case MOSQ_ERR_SUCCESS: // no error
  108. rbReconnect = false;
  109. rbConnPending = false;
  110. bRet = true;
  111. break;
  112. case MOSQ_ERR_CONN_PENDING: // just wait
  113. rbReconnect = false;
  114. rbConnPending = true;
  115. bRet = false;
  116. break;
  117. case MOSQ_ERR_NO_CONN: // (maybe) recoverable errors
  118. case MOSQ_ERR_CONN_LOST:
  119. case MOSQ_ERR_EAI:
  120. rbReconnect = true;
  121. rbConnPending = false;
  122. bRet = false;
  123. break;
  124. case MOSQ_ERR_ERRNO: // system error
  125. switch(errno)
  126. {
  127. case ECONNREFUSED:
  128. case ENETUNREACH:
  129. case ENETRESET:
  130. case ENETDOWN:
  131. case ETIMEDOUT:
  132. case ECONNRESET:
  133. case ECONNABORTED:
  134. case ENOTCONN:
  135. case ESHUTDOWN:
  136. case EHOSTDOWN:
  137. case EHOSTUNREACH:
  138. rbReconnect = true;
  139. rbConnPending = false;
  140. bRet = false;
  141. break;
  142. case EISCONN:
  143. case EALREADY:
  144. case EINPROGRESS:
  145. rbReconnect = false;
  146. rbConnPending = true;
  147. bRet = false;
  148. break;
  149. case EINTR:
  150. rbIntr = true;
  151. // fall through
  152. default:
  153. TRACE("errno: %d\n", errno);
  154. rbReconnect = false;
  155. rbConnPending = false;
  156. bRet = false;
  157. break;
  158. }
  159. break;
  160. default: // unrecoverable errors
  161. rbReconnect = false;
  162. rbConnPending = false;
  163. bRet = false;
  164. break;
  165. }
  166. if(!bRet)
  167. {
  168. const char *pszErr = ::mosquitto_strerror(nErr);
  169. strErr = pszErr;
  170. }
  171. return bRet;
  172. }
  173. bool CMqttClient::TimedLoop(pc_time64_t nLoopTime /* in nano seconds */, int &rnErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr)
  174. {
  175. bool bRet;
  176. int nLoopTimeout;
  177. pc_time64_t nRemaining = nLoopTime;
  178. m_pc.ClockTrigger();
  179. do
  180. {
  181. nLoopTimeout = (int)_min((pc_time64_t)(MQTTCL_DEFAULT_LOOP_TIMEOUT * _PC_NS_PER_MS), nRemaining);
  182. rnErr = loop(nLoopTimeout / _PC_NS_PER_MS);
  183. if(rbIntr)
  184. {
  185. rbReconnect = false;
  186. rbConnPending = false;
  187. bRet = false;
  188. rnErr = MOSQ_ERR_ERRNO;
  189. break;
  190. }
  191. bRet = EvalError(rnErr, rbReconnect, rbConnPending, rbIntr, strErr);
  192. nRemaining = nLoopTime - m_pc.ClockGetElapsed();
  193. if(bRet && !rbReconnect && (nRemaining > (10 * _PC_NS_PER_MS)))
  194. {
  195. if(usleep(10 * _PC_US_PER_MS) < 0)
  196. {
  197. if(errno == EINTR)
  198. {
  199. rbReconnect = false;
  200. rbConnPending = false;
  201. bRet = false;
  202. rnErr = MOSQ_ERR_ERRNO;
  203. rbIntr = true;
  204. break;
  205. }
  206. }
  207. nRemaining = nLoopTime - m_pc.ClockGetElapsed();
  208. }
  209. else
  210. {
  211. break;
  212. }
  213. }
  214. while(nRemaining >= (MQTTCL_DEFAULT_LOOP_TIMEOUT * _PC_NS_PER_MS));
  215. return bRet;
  216. }
  217. /////////////////////////////////////////////////////////////////////////////
  218. //
  219. void CMqttClient::on_connect(int rc)
  220. {
  221. if(m_pEvtCallback && m_evtMask & NEVT_Connect)
  222. {
  223. MQTT_GENERIC_NOTIFICATION ntf;
  224. ntf.evt = NEVT_Connect;
  225. ntf.con.rc = rc;
  226. (*m_pEvtCallback)(&ntf, m_pUserParam);
  227. }
  228. }
  229. void CMqttClient::on_connect_with_flags(int rc, int flags)
  230. {
  231. if(m_pEvtCallback && m_evtMask & NEVT_Connect_With_Flags)
  232. {
  233. MQTT_GENERIC_NOTIFICATION ntf;
  234. ntf.evt = NEVT_Connect_With_Flags;
  235. ntf.con.rc = rc;
  236. ntf.con.flags = flags;
  237. (*m_pEvtCallback)(&ntf, m_pUserParam);
  238. }
  239. }
  240. void CMqttClient::on_disconnect(int rc)
  241. {
  242. if(m_pEvtCallback && m_evtMask & NEVT_Disconnect)
  243. {
  244. MQTT_GENERIC_NOTIFICATION ntf;
  245. ntf.evt = NEVT_Disconnect;
  246. ntf.con.rc = rc;
  247. (*m_pEvtCallback)(&ntf, m_pUserParam);
  248. }
  249. }
  250. void CMqttClient::on_subscribe(int mid, int qos_count, const int *granted_qos)
  251. {
  252. if(m_pEvtCallback && m_evtMask & NEVT_Subscribe)
  253. {
  254. MQTT_GENERIC_NOTIFICATION ntf;
  255. ntf.evt = NEVT_Subscribe;
  256. ntf.sub.mid = mid;
  257. ntf.sub.qos_count = qos_count;
  258. ntf.sub.granted_qos = granted_qos;
  259. (*m_pEvtCallback)(&ntf, m_pUserParam);
  260. }
  261. }
  262. void CMqttClient::on_unsubscribe(int mid)
  263. {
  264. if(m_pEvtCallback && m_evtMask & NEVT_Unsubscribe)
  265. {
  266. MQTT_GENERIC_NOTIFICATION ntf;
  267. ntf.evt = NEVT_Unsubscribe;
  268. ntf.sub.mid = mid;
  269. (*m_pEvtCallback)(&ntf, m_pUserParam);
  270. }
  271. }
  272. void CMqttClient::on_publish(int mid)
  273. {
  274. if(m_pEvtCallback && m_evtMask & NEVT_Publish)
  275. {
  276. MQTT_GENERIC_NOTIFICATION ntf;
  277. ntf.evt = NEVT_Publish;
  278. ntf.pub.mid = mid;
  279. (*m_pEvtCallback)(&ntf, m_pUserParam);
  280. }
  281. }
  282. void CMqttClient::on_message(const struct mosquitto_message *message)
  283. {
  284. CMqttMessage *pMsg = CMqttMessage::CreateMessage(message);
  285. PushRcvMsg(pMsg);
  286. if(m_pEvtCallback && m_evtMask & NEVT_Message)
  287. {
  288. MQTT_GENERIC_NOTIFICATION ntf;
  289. ntf.evt = NEVT_Message;
  290. ntf.msg.msg = pMsg;
  291. (*m_pEvtCallback)(&ntf, m_pUserParam);
  292. }
  293. }
  294. void CMqttClient::on_log(int level, const char *str)
  295. {
  296. if(m_pEvtCallback && m_evtMask & NEVT_Log)
  297. {
  298. MQTT_GENERIC_NOTIFICATION ntf;
  299. ntf.evt = NEVT_Log;
  300. ntf.log.level = level;
  301. ntf.log.str = str;
  302. (*m_pEvtCallback)(&ntf, m_pUserParam);
  303. }
  304. }
  305. void CMqttClient::on_error(void)
  306. {
  307. if(m_pEvtCallback && m_evtMask & NEVT_Error)
  308. {
  309. MQTT_GENERIC_NOTIFICATION ntf;
  310. ntf.evt = NEVT_Error;
  311. (*m_pEvtCallback)(NULL, m_pUserParam);
  312. }
  313. }