qthsdk_demo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- --------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_qthsdk.h"
  17. #include "ql_log.h"
  18. #define QIOT_MQTT_REGISTER_URL "iot-south.quectel.com:2883"
  19. #define QIOT_MQTT_PRODUCT_KEY "p1115X"
  20. #define QIOT_MQTT_PRODUCT_SECRET "d2c5Q1FsVWpwT1k3"
  21. #define QIOT_MCU_COMPONENT_NO "MCU"
  22. #define QIOT_MCU_VERSION "1"
  23. #define QL_QTHSDK_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define QL_QTHSDK_LOG(msg, ...) QL_LOG(QL_QTHSDK_LOG_LEVEL, "ql_QTHSDK", msg, ##__VA_ARGS__)
  25. ql_task_t qthsdk_app_task = NULL;
  26. /**************************************************************************
  27. ** 功能 @brief : 物模型数据遍历
  28. ** 输入 @param :
  29. ** 输出 @retval:
  30. ***************************************************************************/
  31. void Ql_iotTtlvHandle(const void *ttlvHead)
  32. {
  33. quint32_t count = Ql_iotTtlvCountGet(ttlvHead);
  34. quint32_t i;
  35. for(i=0;i<count;i++)
  36. {
  37. uint16_t id;
  38. QIot_dpDataType_e type;
  39. void *node = Ql_iotTtlvNodeGet(ttlvHead, i, &id, &type);
  40. if(node)
  41. {
  42. switch (type)
  43. {
  44. case QIOT_DPDATA_TYPE_BOOL:
  45. {
  46. qbool value;
  47. Ql_iotTtlvNodeGetBool(node, &value);
  48. QL_QTHSDK_LOG("model id:%d data:%s",id,value?"TRUE":"FALSE");
  49. break;
  50. }
  51. case QIOT_DPDATA_TYPE_INT:
  52. {
  53. qint64_t num;
  54. Ql_iotTtlvNodeGetInt(node, &num);
  55. QL_QTHSDK_LOG("model id:%d data:%ld",id,num);
  56. break;
  57. }
  58. case QIOT_DPDATA_TYPE_FLOAT:
  59. {
  60. double num;
  61. Ql_iotTtlvNodeGetFloat(node, &num);
  62. QL_QTHSDK_LOG("model id:%d data:%lg",id,num);
  63. break;
  64. }
  65. case QIOT_DPDATA_TYPE_BYTE:
  66. {
  67. quint8_t *value;
  68. quint32_t len = Ql_iotTtlvNodeGetByte(node, &value);
  69. QL_QTHSDK_LOG("model id:%d data:%.*s",id,len,value);
  70. break;
  71. }
  72. case QIOT_DPDATA_TYPE_STRUCT:
  73. Ql_iotTtlvHandle(Ql_iotTtlvNodeGetStruct(node));
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. /**************************************************************************
  82. ** 功能 @brief : 事件处理回调
  83. ** 输入 @param :
  84. ** 输出 @retval:
  85. ***************************************************************************/
  86. void Ql_iotEventCB(quint32_t event, qint32_t errcode, const void *value, quint32_t valLen)
  87. {
  88. QL_QTHSDK_LOG("event[%d] errcode[%d] valLen[%d]", event, errcode, valLen);
  89. switch (event)
  90. {
  91. /* 引导认证操作 */
  92. case QIOT_ATEVENT_TYPE_AUTH:
  93. QL_QTHSDK_LOG("auth event,code:%d",errcode);
  94. break;
  95. /* 接入操作 */
  96. case QIOT_ATEVENT_TYPE_CONN:
  97. QL_QTHSDK_LOG("connect event,code:%d",errcode);
  98. break;
  99. /* 订阅操作 */
  100. case QIOT_ATEVENT_TYPE_SUBCRIBE:
  101. {
  102. QL_QTHSDK_LOG("subcribe event,code:%d",errcode);
  103. /* 上报设备状态和模组信息 */
  104. quint16_t statusIds[] = {QIOT_DPID_STATUS_BATTERY,
  105. QIOT_DPID_STATUS_VOLTAGE,
  106. QIOT_DPID_STATUS_SIGNAL,
  107. QIOT_DPID_STATUS_FLASHFREE,
  108. QIOT_DPID_STATUS_RSRP,
  109. QIOT_DPID_STATUS_RSRQ,
  110. QIOT_DPID_STATUS_SNR};
  111. quint16_t infoIds[] = {QIOT_DPID_INFO_MODEL_TYPE,
  112. QIOT_DPID_INFO_MODEL_VER,
  113. QIOT_DPID_INFO_MCU_VER,
  114. QIOT_DPID_INFO_CELLID,
  115. QIOT_DPID_INFO_ICCID,
  116. QIOT_DPID_INFO_MCC,
  117. QIOT_DPID_INFO_MNC,
  118. QIOT_DPID_INFO_LAC,
  119. QIOT_DPID_INFO_PHONE_NUM,
  120. QIOT_DPID_INFO_SIM_NUM,
  121. QIOT_DPID_INFO_SDK_VER,
  122. QIOT_DPID_INFO_LOC_SUPLIST,
  123. QIOT_DPIO_INFO_DP_VER,
  124. QIOT_DPIO_INFO_CP_VER};
  125. Ql_iotCmdSysStatusReport(statusIds, sizeof(statusIds) / sizeof(statusIds[0]));
  126. Ql_iotCmdSysDevInfoReport(infoIds, sizeof(infoIds) / sizeof(infoIds[0]));
  127. /* 发送外部定位数据 */
  128. void *nmeaTtlv=NULL;
  129. Ql_iotTtlvIdAddString(&nmeaTtlv,0,"$GPGGA,042523.0,3413.610533,N,10854.063257,E,1,05,2.6,438.5,M,-28.0,M,,*78");
  130. Ql_iotTtlvIdAddString(&nmeaTtlv,0,"$GPRMC,042523.0,A,3413.610533,N,10854.063257,E,0.0,245.9,190716,0.0,E,A*0F");
  131. Ql_iotCmdBusLocReportOutside(nmeaTtlv);
  132. Ql_iotTtlvFree(&nmeaTtlv);
  133. /* 发送内部定位数据 */
  134. void *titleTtlv = NULL;
  135. Ql_iotTtlvIdAddString(&titleTtlv, 0, "LBS");
  136. Ql_iotCmdBusLocReportInside(titleTtlv);
  137. Ql_iotTtlvFree(&titleTtlv);
  138. break;
  139. }
  140. /* 发送数据操作 */
  141. case QIOT_ATEVENT_TYPE_SEND:
  142. QL_QTHSDK_LOG("data send event,code:%d",errcode);
  143. break;
  144. /* 接收数据操作 */
  145. case QIOT_ATEVENT_TYPE_RECV:
  146. QL_QTHSDK_LOG("data recv event,code:%d",errcode);
  147. /* 收到透传数据 */
  148. if(QIOT_RECV_SUCC_TRANS == errcode)
  149. {
  150. QL_QTHSDK_LOG("pass data:%.*s",valLen,(char *)value);
  151. /* 测试,把收到的透传数据回传到平台 */
  152. Ql_iotCmdBusPassTransSend(0,(unsigned char *)value,valLen);
  153. }
  154. /* 收到物模型下发数据 */
  155. else if(QIOT_RECV_SUCC_PHYMODEL_RECV == errcode)
  156. {
  157. /* 测试,把收到的物模型数据回传到平台 */
  158. Ql_iotCmdBusPhymodelReport(0,value);
  159. /* 物模型数据解析 */
  160. Ql_iotTtlvHandle(value);
  161. }
  162. /* 收到物模型请求数据 */
  163. else if(QIOT_RECV_SUCC_PHYMODEL_REQ == errcode && value)
  164. {
  165. quint16_t pkgId = *(quint16_t *)value;
  166. quint16_t *ids = (quint16_t *)(value+sizeof(quint16_t));
  167. void *ttlvHead = NULL;
  168. QL_QTHSDK_LOG("model read event,pkgid:%d",pkgId);
  169. quint32_t i;
  170. for(i=0;i<valLen;i++)
  171. {
  172. quint16_t modelId = ids[i];
  173. QL_QTHSDK_LOG("modelId:%d",modelId);
  174. /* id1:bool id2:int id3:string id4:int array id5:string array*/
  175. switch (modelId)
  176. {
  177. case 1:
  178. Ql_iotTtlvIdAddBool(&ttlvHead, modelId, TRUE);
  179. break;
  180. case 2:
  181. Ql_iotTtlvIdAddInt(&ttlvHead, modelId, 1);
  182. break;
  183. case 3:
  184. Ql_iotTtlvIdAddString(&ttlvHead, modelId, "hello world");
  185. break;
  186. case 4:
  187. {
  188. void *ttlvArrayHead = NULL;
  189. Ql_iotTtlvIdAddInt(&ttlvArrayHead, 1, 1);
  190. Ql_iotTtlvIdAddInt(&ttlvArrayHead, 2, 2);
  191. Ql_iotTtlvIdAddInt(&ttlvArrayHead, 3, 3);
  192. Ql_iotTtlvIdAddStruct(&ttlvHead, modelId, ttlvArrayHead);
  193. }
  194. break;
  195. case 5:
  196. {
  197. void *ttlvArrayHead = NULL;
  198. Ql_iotTtlvIdAddString(&ttlvArrayHead, 1, "hello a");
  199. Ql_iotTtlvIdAddString(&ttlvArrayHead, 2, "hello b");
  200. Ql_iotTtlvIdAddString(&ttlvArrayHead, 3, "hello c");
  201. Ql_iotTtlvIdAddStruct(&ttlvHead, modelId, ttlvArrayHead);
  202. }
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. Ql_iotCmdBusPhymodelAck(0,pkgId,ttlvHead);
  209. Ql_iotTtlvFree(&ttlvHead);
  210. }
  211. break;
  212. /* 注销操作 */
  213. case QIOT_ATEVENT_TYPE_LOGOUT:
  214. QL_QTHSDK_LOG("logout event,code:%d",errcode);
  215. break;
  216. /* FOTA操作 */
  217. case QIOT_ATEVENT_TYPE_OTA:
  218. {
  219. switch (errcode)
  220. {
  221. /* 下发升级任务 */
  222. case QIOT_OTA_TASK_NOTIFY:
  223. {
  224. if(NULL != value)
  225. {
  226. QL_QTHSDK_LOG("recv ota task:%s", (char *)value);
  227. /* QL_QTHSDK_LOG("componentNo:%s,sourceVersion:%s,targetVersion:%s,batteryLimit:%s,minSignalIntensity:%s,minSignalIntensity:%s", \
  228. words[0],words[1],words[2],words[3],words[4],words[5]); */
  229. /* 确认升级 */
  230. Ql_iotCmdOtaAction(1);
  231. }
  232. break;
  233. }
  234. /* 开始下载 */
  235. case QIOT_OTA_START:
  236. QL_QTHSDK_LOG("ota download start");
  237. break;
  238. /* 下载中 */
  239. case QIOT_OTA_DOWNLOADING:
  240. QL_QTHSDK_LOG("ota downloading");
  241. break;
  242. /* 下载完成 */
  243. case QIOT_OTA_DOWNLOADED:
  244. {
  245. if(NULL != value)
  246. {
  247. QL_QTHSDK_LOG("ota download complete:%s", (char *)value);
  248. /* QL_QTHSDK_LOG("componentNo:%s,length:%s,startaddr:%s,piece_length:%s",words[0],words[1],words[2],words[3]); */
  249. /* 如果是SOTA下载完成,则通过API读取文件 */
  250. /* if(strcmp(QIOT_MCU_COMPONENT_NO, words[0]) == 0) */
  251. if(NULL != strstr((char *)value /* words[0] */, QIOT_MCU_COMPONENT_NO))
  252. {
  253. quint8_t readBuf[1024];
  254. quint32_t ret = Ql_iotCmdOtaMcuFWDataRead(0,readBuf,sizeof(readBuf));
  255. QL_QTHSDK_LOG("sota read file...ret:%d",ret);
  256. /* SOTA完成后通知云平台MCU进入更新状态 */
  257. Ql_iotCmdOtaAction(3);
  258. }
  259. }
  260. break;
  261. }
  262. /* 更新中 */
  263. case QIOT_OTA_UPDATING:
  264. QL_QTHSDK_LOG("ota updating");
  265. break;
  266. /* 更新完成 */
  267. case QIOT_OTA_UPDATE_OK:
  268. QL_QTHSDK_LOG("ota update success");
  269. break;
  270. /* 更新失败 */
  271. case QIOT_OTA_UPDATE_FAIL:
  272. QL_QTHSDK_LOG("ota update fail");
  273. break;
  274. default:
  275. break;
  276. }
  277. break;
  278. }
  279. /* 平台事件 */
  280. case QIOT_ATEVENT_TYPE_SERVER:
  281. QL_QTHSDK_LOG("server event,code:%d",errcode);
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. static void qthsdk_app_thread(void * arg)
  288. {
  289. ql_rtos_task_sleep_s(10);
  290. QL_QTHSDK_LOG("========== qthsdk demo start ==========");
  291. /* 初始化qucsdk */
  292. Ql_iotInit();
  293. /* 设置软件版本标识 */
  294. Ql_iotConfigSetAppVersion("app");
  295. /* 注册事件回调函数 */
  296. Ql_iotConfigSetEventCB(Ql_iotEventCB);
  297. /* 配置产品信息*/
  298. Ql_iotConfigSetProductinfo(QIOT_MQTT_PRODUCT_KEY, QIOT_MQTT_PRODUCT_SECRET);
  299. /* 配置服务器信息,可选,默认连接MQTT生产环境服务器 */
  300. Ql_iotConfigSetServer(QIOT_PPROTOCOL_MQTT,QIOT_MQTT_REGISTER_URL);
  301. /* 配置PDP context Id,可选,默认为1 */
  302. Ql_iotConfigSetPdpContextId(1);
  303. /* 配置lifetime,可选,MQTT默认为120 */
  304. Ql_iotConfigSetLifetime(120);
  305. /* 配置外部MCU标识号和版本号,可选,如没有外部MCU则不需要配置 */
  306. Ql_iotConfigSetMcuVersion(QIOT_MCU_COMPONENT_NO,QIOT_MCU_VERSION);
  307. /* 启动云平台连接 */
  308. Ql_iotConfigSetConnmode(QIOT_CONNMODE_REQ);
  309. while (1)
  310. {
  311. QIot_state_e status = Ql_iotGetWorkState();
  312. QL_QTHSDK_LOG("work status:%d",status);
  313. ql_rtos_task_sleep_s(100);
  314. }
  315. }
  316. void ql_qthsdk_app_init(void)
  317. {
  318. QlOSStatus err = QL_OSI_SUCCESS;
  319. err = ql_rtos_task_create(&qthsdk_app_task, 4096, APP_PRIORITY_NORMAL, "qthsdk_app", qthsdk_app_thread, NULL, 5);
  320. if(err != QL_OSI_SUCCESS)
  321. {
  322. QL_QTHSDK_LOG("created task failed");
  323. }
  324. }