iot_cloud.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "iot_cloud.h"
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include "ohos_init.h"
  22. #include "cmsis_os2.h"
  23. #include <cJSON.h>
  24. #include <dtls_al.h>
  25. #include <mqtt_al.h>
  26. #include <oc_mqtt_al.h>
  27. #include <oc_mqtt_profile.h>
  28. #include <queue.h>
  29. // we use this function to send the data here
  30. extern int OcMqttReportString(char *deviceID, const char *jsonString);
  31. extern int OcMqttMsgUpString(char *deviceID, const char *jsonString);
  32. typedef enum {
  33. EN_MSG_TYPE_CMD = 0,
  34. EN_MSG_TYPE_REPORT,
  35. EN_MSG_TYPE_CONN,
  36. EN_MSG_TYPE_DISCONN,
  37. EN_MSG_TYPE_NOTIFY,
  38. }CloudMsgType;
  39. typedef struct
  40. {
  41. char *requestID;
  42. char *payload;
  43. }CloudCommandMsg;
  44. typedef struct {
  45. char *jsonString;
  46. }CloudReportMsg;
  47. typedef struct {
  48. CloudMsgType msgType;
  49. union {
  50. CloudCommandMsg cmd;
  51. CloudReportMsg report;
  52. CloudReportMsg notify;
  53. }msg;
  54. }ApplicationMsg;
  55. typedef struct
  56. {
  57. osMessageQueueId_t applicationMsgQueue;
  58. int connected;
  59. osThreadId_t mainTaskID;
  60. }CloudController;
  61. #define CONFIG_APP_LIFETIME 60 ///< seconds
  62. #define CONFIG_QUEUE_GET_TIMEOUT 0xffffffff
  63. #define CONFIG_QUEUE_PUSH_TIMEOUT 5000
  64. #define CLOUD_TASK_DELAY 40
  65. #define MSGQUEUE_OBJECTS 10
  66. static CloudController g_cloudController;
  67. static void HuaweiIoTSDKInit(void)
  68. {
  69. dtls_al_init();
  70. mqtt_al_init();
  71. oc_mqtt_init();
  72. }
  73. /**
  74. * @brief This function used to report the data to the cloud
  75. *
  76. */
  77. static void DealReportMsg(CloudReportMsg *reportMsg)
  78. {
  79. OcMqttReportString(NULL, reportMsg->jsonString);
  80. return;
  81. }
  82. static void DealNotificationMsg(CloudReportMsg *notifyMsg)
  83. {
  84. OcMqttMsgUpString(NULL, notifyMsg->jsonString);
  85. }
  86. static void DealCommandMsg(CloudCommandMsg *cmd);
  87. /**
  88. * @brief This function used to deal with message received from the cloud
  89. * we package the received data to the queue to do next step
  90. */
  91. static int CloudMsgRcvCallBack(oc_mqtt_profile_msgrcv_t *msg)
  92. {
  93. int ret = 0;
  94. char *buf = NULL;
  95. int bufLen;
  96. ApplicationMsg *applicationMsg = NULL;
  97. if ((NULL == msg) || (msg->request_id == NULL) || (msg->type != EN_OC_MQTT_PROFILE_MSG_TYPE_DOWN_COMMANDS)) {
  98. RaiseLog(LOG_LEVEL_WARN,"Parameter is wrong format\n");
  99. return ret;
  100. }
  101. bufLen = sizeof(ApplicationMsg) + strlen(msg->request_id) + 1 + msg->msg_len + 1;
  102. buf = malloc(bufLen);
  103. if (NULL == buf) {
  104. RaiseLog(LOG_LEVEL_ERR,"No memory for the command buffer\n");
  105. return ret;
  106. }
  107. applicationMsg = (ApplicationMsg *)buf;
  108. buf += sizeof(ApplicationMsg);
  109. applicationMsg->msgType = EN_MSG_TYPE_CMD;
  110. applicationMsg->msg.cmd.requestID = buf;
  111. bufLen = strlen(msg->request_id);
  112. buf += bufLen + 1;
  113. memcpy(applicationMsg->msg.cmd.requestID, msg->request_id, bufLen);
  114. applicationMsg->msg.cmd.requestID[bufLen] = '\0';
  115. bufLen = msg->msg_len;
  116. applicationMsg->msg.cmd.payload = buf;
  117. memcpy(applicationMsg->msg.cmd.payload, msg->msg, bufLen);
  118. applicationMsg->msg.cmd.payload[bufLen] = '\0';
  119. RaiseLog(LOG_LEVEL_INFO,"GetCommand:reqID:%s payload:%s \n", \
  120. applicationMsg->msg.cmd.requestID, applicationMsg->msg.cmd.payload);
  121. ret = osMessageQueuePut(g_cloudController.applicationMsgQueue,(void**)&applicationMsg,NULL,CONFIG_QUEUE_PUSH_TIMEOUT);
  122. if (ret != 0){
  123. free(applicationMsg);
  124. RaiseLog(LOG_LEVEL_ERR,"Push the message to the queue failed");
  125. }
  126. return ret;
  127. }
  128. static void DealCommandMsg(CloudCommandMsg *cmd)
  129. {
  130. oc_mqtt_profile_cmdresp_t cmdresp;
  131. int ret = CLOUD_CommandCallBack((const char *)(cmd->payload));
  132. ///< do the response
  133. cmdresp.paras = NULL;
  134. cmdresp.request_id = cmd->requestID;
  135. cmdresp.ret_code = (ret == 0 ? 0 : 1);
  136. cmdresp.ret_name = NULL;
  137. (void)oc_mqtt_profile_cmdresp(NULL, &cmdresp);
  138. return;
  139. }
  140. /**
  141. * @brief this is the cloud main task entry
  142. * we deal all the message in the queue
  143. */
  144. static int CloudMainTaskEntry(void *arg)
  145. {
  146. ApplicationMsg* applicationMsg = NULL;
  147. uint32_t ret ;
  148. // receive the message from the queue ,maybe receive from the clould, or maybe from the local
  149. while (1) {
  150. applicationMsg = NULL;
  151. (void)osMessageQueueGet(g_cloudController.applicationMsgQueue,(void **)&applicationMsg,NULL,CONFIG_QUEUE_GET_TIMEOUT);
  152. if (applicationMsg != NULL) {
  153. RaiseLog(LOG_LEVEL_INFO,"GetMsgType:%d",applicationMsg->msgType);
  154. switch (applicationMsg->msgType){
  155. case EN_MSG_TYPE_CMD:
  156. DealCommandMsg(&applicationMsg->msg.cmd);
  157. break;
  158. case EN_MSG_TYPE_REPORT:
  159. DealReportMsg(&applicationMsg->msg.report);
  160. break;
  161. case EN_MSG_TYPE_NOTIFY:
  162. DealNotificationMsg(&applicationMsg->msg.notify);
  163. break;
  164. default:
  165. break;
  166. }
  167. free(applicationMsg);
  168. }else {
  169. RaiseLog(LOG_LEVEL_INFO,"osMessageQueueGet failed");
  170. }
  171. osDelay(CLOUD_TASK_DELAY);
  172. }
  173. return 0;
  174. }
  175. int CLOUD_Init(void)
  176. {
  177. int ret = -1;
  178. /* create a queue to buffer the data */
  179. printf("Could init begin\n");
  180. g_cloudController.applicationMsgQueue = osMessageQueueNew(MSGQUEUE_OBJECTS, sizeof(ApplicationMsg*), NULL);
  181. if(NULL == g_cloudController.applicationMsgQueue){
  182. printf("Create receive msg queue failed");
  183. return ret;
  184. }
  185. /* initialize the iot sdk */
  186. HuaweiIoTSDKInit();
  187. /* create a task to deal the send message or received message */
  188. osThreadAttr_t attr;
  189. attr.attr_bits = 0U;
  190. attr.cb_mem = NULL;
  191. attr.cb_size = 0U;
  192. attr.stack_mem = NULL;
  193. attr.name = "IoTCloudMain";
  194. attr.stack_size = 1024*8;
  195. attr.priority = 30;
  196. printf("Could Thread init begin\n");
  197. if ((g_cloudController.mainTaskID = osThreadNew((osThreadFunc_t)CloudMainTaskEntry, NULL, &attr)) == NULL) {
  198. printf("Could not create the iotcloud thread");
  199. return ret;
  200. }
  201. ret = 0;
  202. printf("Could init success\n");
  203. return ret;
  204. }
  205. int CLOUD_Deinit(void)
  206. {
  207. int ret = -1;
  208. osThreadTerminate(g_cloudController.mainTaskID);
  209. g_cloudController.mainTaskID = NULL;
  210. osMessageQueueDelete( g_cloudController.applicationMsgQueue);
  211. g_cloudController.applicationMsgQueue = NULL;
  212. return ret;
  213. }
  214. int CLOUD_Connect(const char *deviceID, const char *devicePwd, \
  215. const char *serverIP, const char *serverPort)
  216. {
  217. int ret;
  218. oc_mqtt_profile_connect_t connect_para;
  219. (void) memset( &connect_para, 0, sizeof(connect_para));
  220. connect_para.boostrap = 0;
  221. connect_para.device_id = (char *)deviceID;
  222. connect_para.device_passwd = (char *)devicePwd;
  223. connect_para.server_addr = (char *)serverIP;
  224. connect_para.server_port = (char *)serverPort;
  225. connect_para.life_time = CONFIG_APP_LIFETIME;
  226. connect_para.rcvfunc = CloudMsgRcvCallBack;
  227. connect_para.security.type = EN_DTLS_AL_SECURITY_TYPE_NONE;
  228. ret = oc_mqtt_profile_connect(&connect_para);
  229. if ((ret == (int)en_oc_mqtt_err_ok)) {
  230. g_cloudController.connected = 1;
  231. ret = 0;
  232. }
  233. else {
  234. RaiseLog(LOG_LEVEL_ERR,"Could not connect to the huaweiIotPlatform:deviceID:%s devicePwd:%s serverIP:%s serverPort:%s", \
  235. deviceID, devicePwd, serverIP, serverPort);
  236. ret = -1;
  237. }
  238. return ret;
  239. }
  240. int CLOUD_Disconnect(void)
  241. {
  242. int ret;
  243. ret = oc_mqtt_profile_disconnect();
  244. if (ret == (int)en_oc_mqtt_err_ok) {
  245. return 0;
  246. } else {
  247. return -1;
  248. }
  249. }
  250. int CLOUD_ReportMsg(const char *jsonString)
  251. {
  252. int ret = -1;
  253. ApplicationMsg *applicationMsg = NULL;
  254. if (jsonString == NULL) {
  255. RaiseLog(LOG_LEVEL_WARN,"Parameter jsonString is NULL");
  256. return ret;
  257. }
  258. applicationMsg = malloc(sizeof(ApplicationMsg) + strlen(jsonString) + 1);
  259. if (NULL != applicationMsg) {
  260. applicationMsg->msgType = EN_MSG_TYPE_REPORT;
  261. applicationMsg->msg.report.jsonString = (char *)applicationMsg + sizeof(ApplicationMsg);
  262. strcpy(applicationMsg->msg.report.jsonString, jsonString);
  263. if (0 != osMessageQueuePut(g_cloudController.applicationMsgQueue, (void **)&applicationMsg, NULL,CONFIG_QUEUE_PUSH_TIMEOUT)) {
  264. RaiseLog(LOG_LEVEL_ERR,"Could not push message to the message queue");
  265. } else {
  266. ret = 0;
  267. }
  268. } else {
  269. RaiseLog(LOG_LEVEL_ERR,"Could not get the memory for the application message");
  270. }
  271. free(applicationMsg);
  272. return ret;
  273. }
  274. int CLOUD_ReportNotification(int type, const char *enString, const char *chString)
  275. {
  276. int ret = -1;
  277. ApplicationMsg *applicationMsg = NULL;
  278. char *jsonString = NULL;
  279. if (type >= NOTIFY_TYPE_LAST || enString == NULL || chString == NULL) {
  280. RaiseLog(LOG_LEVEL_WARN,"Parameter failed!\n");
  281. return -1;
  282. }
  283. jsonString = IotNotificationPackage(type, enString, chString);
  284. if (jsonString == NULL) {
  285. RaiseLog(LOG_LEVEL_WARN,"IotNotificationPackage jsonString failed! \n");
  286. return ret;
  287. }
  288. applicationMsg = malloc(sizeof(ApplicationMsg) + strlen(jsonString) + 1);
  289. if (NULL != applicationMsg) {
  290. applicationMsg->msgType = EN_MSG_TYPE_NOTIFY;
  291. applicationMsg->msg.report.jsonString = (char *)applicationMsg + sizeof(ApplicationMsg);
  292. strcpy(applicationMsg->msg.report.jsonString, jsonString);
  293. if (0 != osMessageQueuePut(g_cloudController.applicationMsgQueue, (void**)&applicationMsg, NULL,CONFIG_QUEUE_PUSH_TIMEOUT)) {
  294. free(applicationMsg);
  295. RaiseLog(LOG_LEVEL_ERR,"Could not push message to the message queue");
  296. } else {
  297. ret = 0;
  298. }
  299. } else {
  300. RaiseLog(LOG_LEVEL_ERR,"Could not get the memory for the application message");
  301. }
  302. free(jsonString);
  303. return ret;
  304. }
  305. int CLOUD_GetCloudConnectedStatus(void)
  306. {
  307. return g_cloudController.connected;
  308. }