iot_cloud.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. //RaiseLog(LOG_LEVEL_INFO,"**********+++++++ret**********+++++++:%s\r\n",ret);
  123. if (ret != 0){
  124. free(applicationMsg);
  125. RaiseLog(LOG_LEVEL_ERR,"Push the message to the queue failed");
  126. }
  127. return ret;
  128. }
  129. static void DealCommandMsg(CloudCommandMsg *cmd)
  130. {
  131. oc_mqtt_profile_cmdresp_t cmdresp;
  132. int ret = CLOUD_CommandCallBack((const char *)(cmd->payload));
  133. ///< do the response
  134. cmdresp.paras = NULL;
  135. cmdresp.request_id = cmd->requestID;
  136. cmdresp.ret_code = (ret == 0 ? 0 : 1);
  137. cmdresp.ret_name = NULL;
  138. (void)oc_mqtt_profile_cmdresp(NULL, &cmdresp);
  139. return;
  140. }
  141. /**
  142. * @brief this is the cloud main task entry
  143. * we deal all the message in the queue
  144. */
  145. static int CloudMainTaskEntry(void *arg)
  146. {
  147. ApplicationMsg* applicationMsg = NULL;
  148. uint32_t ret ;
  149. // receive the message from the queue ,maybe receive from the clould, or maybe from the local
  150. while (1) {
  151. applicationMsg = NULL;
  152. (void)osMessageQueueGet(g_cloudController.applicationMsgQueue,(void **)&applicationMsg,NULL,CONFIG_QUEUE_GET_TIMEOUT);
  153. if (applicationMsg != NULL) {
  154. RaiseLog(LOG_LEVEL_INFO,"GetMsgType:%d",applicationMsg->msgType);
  155. switch (applicationMsg->msgType){
  156. case EN_MSG_TYPE_CMD:
  157. DealCommandMsg(&applicationMsg->msg.cmd);
  158. break;
  159. case EN_MSG_TYPE_REPORT:
  160. DealReportMsg(&applicationMsg->msg.report);
  161. break;
  162. case EN_MSG_TYPE_NOTIFY:
  163. DealNotificationMsg(&applicationMsg->msg.notify);
  164. break;
  165. default:
  166. break;
  167. }
  168. free(applicationMsg);
  169. }else {
  170. RaiseLog(LOG_LEVEL_INFO,"osMessageQueueGet failed");
  171. }
  172. osDelay(CLOUD_TASK_DELAY);
  173. }
  174. RaiseLog(LOG_LEVEL_INFO,"*********************CloudMainTaskEntry complete************");
  175. return 0;
  176. }
  177. int CLOUD_Init(void)
  178. {
  179. int ret = -1;
  180. /* create a queue to buffer the data */
  181. printf("Could init begin\n");
  182. g_cloudController.applicationMsgQueue = osMessageQueueNew(MSGQUEUE_OBJECTS, sizeof(ApplicationMsg*), NULL);
  183. if(NULL == g_cloudController.applicationMsgQueue){
  184. printf("Create receive msg queue failed");
  185. return ret;
  186. }
  187. /* initialize the iot sdk */
  188. HuaweiIoTSDKInit();
  189. /* create a task to deal the send message or received message */
  190. osThreadAttr_t attr;
  191. attr.attr_bits = 0U;
  192. attr.cb_mem = NULL;
  193. attr.cb_size = 0U;
  194. attr.stack_mem = NULL;
  195. attr.name = "IoTCloudMain";
  196. attr.stack_size = 1024*8;
  197. attr.priority = 30;
  198. printf("Could Thread init begin\n");
  199. if ((g_cloudController.mainTaskID = osThreadNew((osThreadFunc_t)CloudMainTaskEntry, NULL, &attr)) == NULL) {
  200. printf("Could not create the iotcloud thread");
  201. return ret;
  202. }
  203. ret = 0;
  204. printf("Could init success\n");
  205. return ret;
  206. }
  207. int CLOUD_Deinit(void)
  208. {
  209. int ret = -1;
  210. osThreadTerminate(g_cloudController.mainTaskID);
  211. g_cloudController.mainTaskID = NULL;
  212. osMessageQueueDelete( g_cloudController.applicationMsgQueue);
  213. g_cloudController.applicationMsgQueue = NULL;
  214. return ret;
  215. }
  216. int CLOUD_Connect(const char *deviceID, const char *devicePwd, \
  217. const char *serverIP, const char *serverPort)
  218. {
  219. int ret;
  220. oc_mqtt_profile_connect_t connect_para;
  221. (void) memset( &connect_para, 0, sizeof(connect_para));
  222. connect_para.boostrap = 0;
  223. connect_para.device_id = (char *)deviceID;
  224. connect_para.device_passwd = (char *)devicePwd;
  225. connect_para.server_addr = (char *)serverIP;
  226. connect_para.server_port = (char *)serverPort;
  227. connect_para.life_time = CONFIG_APP_LIFETIME;
  228. connect_para.rcvfunc = CloudMsgRcvCallBack;
  229. connect_para.security.type = EN_DTLS_AL_SECURITY_TYPE_NONE;
  230. ret = oc_mqtt_profile_connect(&connect_para);
  231. if ((ret == (int)en_oc_mqtt_err_ok)) {
  232. g_cloudController.connected = 1;
  233. ret = 0;
  234. }
  235. else {
  236. RaiseLog(LOG_LEVEL_ERR,"Could not connect to the huaweiIotPlatform:deviceID:%s devicePwd:%s serverIP:%s serverPort:%s", \
  237. deviceID, devicePwd, serverIP, serverPort);
  238. ret = -1;
  239. }
  240. return ret;
  241. }
  242. int CLOUD_Disconnect(void)
  243. {
  244. printf("@@@@#### int CLOUD_Disconnect(void)\r\n");
  245. int ret;
  246. ret = oc_mqtt_profile_disconnect();
  247. if (ret == (int)en_oc_mqtt_err_ok) {
  248. return 0;
  249. } else {
  250. return -1;
  251. }
  252. }
  253. int CLOUD_ReportMsg(const char *jsonString)
  254. {
  255. int ret = -1;
  256. ApplicationMsg *applicationMsg = NULL;
  257. if (jsonString == NULL) {
  258. RaiseLog(LOG_LEVEL_WARN,"Parameter jsonString is NULL");
  259. return ret;
  260. }
  261. applicationMsg = malloc(sizeof(ApplicationMsg) + strlen(jsonString) + 1);
  262. if (NULL != applicationMsg) {
  263. applicationMsg->msgType = EN_MSG_TYPE_REPORT;
  264. applicationMsg->msg.report.jsonString = (char *)applicationMsg + sizeof(ApplicationMsg);
  265. strcpy(applicationMsg->msg.report.jsonString, jsonString);
  266. if (0 != osMessageQueuePut(g_cloudController.applicationMsgQueue, (void **)&applicationMsg, NULL,CONFIG_QUEUE_PUSH_TIMEOUT)) {
  267. RaiseLog(LOG_LEVEL_ERR,"Could not push message to the message queue");
  268. } else {
  269. ret = 0;
  270. }
  271. } else {
  272. RaiseLog(LOG_LEVEL_ERR,"Could not get the memory for the application message");
  273. }
  274. free(applicationMsg);
  275. return ret;
  276. }
  277. int CLOUD_ReportNotification(int type, const char *enString, const char *chString)
  278. {
  279. int ret = -1;
  280. ApplicationMsg *applicationMsg = NULL;
  281. char *jsonString = NULL;
  282. if (type >= NOTIFY_TYPE_LAST || enString == NULL || chString == NULL) {
  283. RaiseLog(LOG_LEVEL_WARN,"Parameter failed!\n");
  284. return -1;
  285. }
  286. jsonString = IotNotificationPackage(type, enString, chString);
  287. if (jsonString == NULL) {
  288. RaiseLog(LOG_LEVEL_WARN,"IotNotificationPackage jsonString failed! \n");
  289. return ret;
  290. }
  291. applicationMsg = malloc(sizeof(ApplicationMsg) + strlen(jsonString) + 1);
  292. if (NULL != applicationMsg) {
  293. applicationMsg->msgType = EN_MSG_TYPE_NOTIFY;
  294. applicationMsg->msg.report.jsonString = (char *)applicationMsg + sizeof(ApplicationMsg);
  295. strcpy(applicationMsg->msg.report.jsonString, jsonString);
  296. if (0 != osMessageQueuePut(g_cloudController.applicationMsgQueue, (void**)&applicationMsg, NULL,CONFIG_QUEUE_PUSH_TIMEOUT)) {
  297. free(applicationMsg);
  298. RaiseLog(LOG_LEVEL_ERR,"Could not push message to the message queue");
  299. } else {
  300. ret = 0;
  301. }
  302. } else {
  303. RaiseLog(LOG_LEVEL_ERR,"Could not get the memory for the application message");
  304. }
  305. free(jsonString);
  306. return ret;
  307. }
  308. int CLOUD_GetCloudConnectedStatus(void)
  309. {
  310. return g_cloudController.connected;
  311. }