LcmCommandDriver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * LcmCommandDriver.c
  3. *
  4. * Created on: 2022/3/31
  5. * Author: folus
  6. */
  7. #include "Module_LcmControl_Wistron.h"
  8. /**
  9. *
  10. * @param mosq
  11. * @param topic
  12. * @param outputStr
  13. * @return
  14. */
  15. int publish_data(struct mosquitto *mosq, char *topic, char *outputStr)
  16. {
  17. int result;
  18. result = mosquitto_publish(mosq, NULL, topic, strlen(outputStr), outputStr, QOS_ENSURE_BROKER, RETAIN_NO);
  19. if(result != MOSQ_ERR_SUCCESS)DEBUG_ERROR("Publish %s error publishing: %s\n", topic, mosquitto_strerror(result));
  20. return (result != MOSQ_ERR_SUCCESS)?FAIL:PASS;
  21. }
  22. /**
  23. *
  24. * @param mosq
  25. */
  26. int publish_profile(struct mosquitto *mosq, char *parameter, char *value)
  27. {
  28. json_object *payload = json_object_new_object();
  29. char outputStr[2048]={0};
  30. json_object_object_add(payload, parameter, json_object_new_string(value));
  31. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  32. json_object_put(payload);
  33. return publish_data(mosq, "client/profile", outputStr);
  34. }
  35. /**
  36. *
  37. * @param mosq
  38. */
  39. int publish_upgrade(struct mosquitto *mosq, char *otaType, char *url, char *checksum, char *verInfo)
  40. {
  41. json_object *payload = json_object_new_object();
  42. char outputStr[2048]={0};
  43. json_object_object_add(payload, "task_name", json_object_new_string(otaType));
  44. if((strstr(otaType, OTA_TYPE_APK) != NULL) || (strstr(otaType, OTA_TYPE_UI) != NULL))
  45. json_object_object_add(payload, "ftp server", json_object_new_string(url));
  46. if(strstr(otaType, OTA_TYPE_UI) != NULL)
  47. {
  48. json_object_object_add(payload, "file_checksum", json_object_new_string(checksum));
  49. json_object_object_add(payload, "version_info", json_object_new_string(verInfo));
  50. }
  51. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  52. json_object_put(payload);
  53. return publish_data(mosq, "client/command", outputStr);
  54. }
  55. /**
  56. *
  57. * @param mosq
  58. */
  59. int publish_restart(struct mosquitto *mosq, uint8_t isHardReset)
  60. {
  61. json_object *payload = json_object_new_object();
  62. char outputStr[2048]={0};
  63. json_object_object_add(payload, "task_name", json_object_new_string(isHardReset?"restart":"reset"));
  64. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  65. json_object_put(payload);
  66. return publish_data(mosq, "client/command", outputStr);
  67. }
  68. /**
  69. *
  70. * @param mosq
  71. */
  72. int publish_power_saving(struct mosquitto *mosq, uint8_t isSleep)
  73. {
  74. if(isSleep)
  75. {
  76. json_object *payload = json_object_new_object();
  77. char outputStr[2048]={0};
  78. json_object_object_add(payload, "task_name", json_object_new_string("power_saving"));
  79. json_object_object_add(payload, "mode", json_object_new_string(isSleep?"1":"0"));
  80. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  81. json_object_put(payload);
  82. return publish_data(mosq, "client/command", outputStr);
  83. }
  84. else
  85. {
  86. char cmd[64];
  87. sprintf(cmd, "/usr/sbin/ether-wake -i eth0 %s", clientInfo.macAddr);
  88. system(cmd);
  89. return PASS;
  90. }
  91. }
  92. /**
  93. *
  94. * @param mosq
  95. */
  96. int publish_back_dimming(struct mosquitto *mosq, uint8_t lightLevel)
  97. {
  98. json_object *payload = json_object_new_object();
  99. char outputStr[2048]={0};
  100. char buf[8];
  101. json_object_object_add(payload, "task_name", json_object_new_string("backlight"));
  102. sprintf(buf, "%d", lightLevel);
  103. json_object_object_add(payload, "brightness", json_object_new_string(buf));
  104. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  105. json_object_put(payload);
  106. return publish_data(mosq, "client/command", outputStr);
  107. }
  108. /**
  109. *
  110. * @param mosq
  111. */
  112. int publish_audio_volume(struct mosquitto *mosq, uint8_t volume)
  113. {
  114. json_object *payload = json_object_new_object();
  115. char outputStr[2048]={0};
  116. char buf[8];
  117. json_object_object_add(payload, "task_name", json_object_new_string("audio_volume"));
  118. sprintf(buf, "%d", volume);
  119. json_object_object_add(payload, "volume", json_object_new_string(buf));
  120. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  121. json_object_put(payload);
  122. return publish_data(mosq, "client/command", outputStr);
  123. }
  124. /**
  125. *
  126. * @param mosq
  127. */
  128. int publish_trigger_report_status(struct mosquitto *mosq)
  129. {
  130. json_object *payload = json_object_new_object();
  131. char outputStr[2048]={0};
  132. json_object_object_add(payload, "task_name", json_object_new_string("report_status"));
  133. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  134. json_object_put(payload);
  135. return publish_data(mosq, "client/command", outputStr);
  136. }
  137. /**
  138. *
  139. * @param mosq
  140. */
  141. int publish_timesync(struct mosquitto *mosq)
  142. {
  143. char outputStr[64]={0};
  144. time_t CurrentTime;
  145. struct tm *tm;
  146. struct timeval tv;
  147. CurrentTime = time(NULL);
  148. tm=localtime(&CurrentTime);
  149. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  150. sprintf(outputStr,"%04d-%02d-%02dT%02d:%02d:%02d+00:00", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  151. return publish_data(mosq, "client/timesync", outputStr);
  152. }
  153. /**
  154. *
  155. * @param mosq
  156. */
  157. int publish_textview_add(struct mosquitto *mosq, Text_List *textList, uint8_t listCount)
  158. {
  159. json_object *payload = json_object_new_array();
  160. char outputStr[2048]={0};
  161. for(int idx=0;idx<listCount;idx++)
  162. {
  163. json_object *textview = json_object_new_object();
  164. char buf[32];
  165. sprintf(buf, "%d", textList[idx].textviewIndex);
  166. json_object_object_add(textview, "textviewIndex", json_object_new_string(buf));
  167. json_object_object_add(textview, "textString", json_object_new_string(textList[idx].textString));
  168. json_object_object_add(textview, "textFont", json_object_new_string(textList[idx].textFont));
  169. json_object_object_add(textview, "textStyle", json_object_new_string(textList[idx].textStyle));
  170. sprintf(buf, "%d", textList[idx].textSize);
  171. json_object_object_add(textview, "textSize", json_object_new_string(buf));
  172. sprintf(buf, "%d", textList[idx].layout_x);
  173. json_object_object_add(textview, "layout_x", json_object_new_string(buf));
  174. sprintf(buf, "%d", textList[idx].layout_y);
  175. json_object_object_add(textview, "layout_y", json_object_new_string(buf));
  176. json_object_array_add(payload, textview);
  177. }
  178. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  179. json_object_put(payload);
  180. return publish_data(mosq, "client/layout/textview", outputStr);
  181. }
  182. /**
  183. *
  184. * @param mosq
  185. */
  186. int publish_imageview_add(struct mosquitto *mosq, Image_List *imageList, uint8_t listCount)
  187. {
  188. json_object *payload = json_object_new_array();
  189. char outputStr[2048]={0};
  190. for(int idx=0;idx<listCount;idx++)
  191. {
  192. json_object *imageview = json_object_new_object();
  193. char buf[32];
  194. sprintf(buf, "%d", imageList[idx].imageviewIndex);
  195. json_object_object_add(imageview, "imageviewIndex", json_object_new_string(buf));
  196. sprintf(buf, "%d", imageList[idx].imgsrc_addr);
  197. json_object_object_add(imageview, "imgsrc_addr", json_object_new_string(buf));
  198. sprintf(buf, "%d", imageList[idx].layout_x);
  199. json_object_object_add(imageview, "layout_x", json_object_new_string(buf));
  200. sprintf(buf, "%d", imageList[idx].layout_y);
  201. json_object_object_add(imageview, "layout_y", json_object_new_string(buf));
  202. sprintf(buf, "%d", imageList[idx].width);
  203. json_object_object_add(imageview, "width", json_object_new_string(buf));
  204. sprintf(buf, "%d", imageList[idx].height);
  205. json_object_object_add(imageview, "height", json_object_new_string(buf));
  206. json_object_array_add(payload, imageview);
  207. }
  208. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  209. json_object_put(payload);
  210. return publish_data(mosq, "client/layout/imageview", outputStr);
  211. }
  212. /**
  213. *
  214. * @param mosq
  215. */
  216. int publish_videoview_add(struct mosquitto *mosq, Video_List *videoList, uint8_t listCount)
  217. {
  218. json_object *payload = json_object_new_array();
  219. char outputStr[2048]={0};
  220. for(int idx=0;idx<listCount;idx++)
  221. {
  222. json_object *imageview = json_object_new_object();
  223. char buf[32];
  224. sprintf(buf, "%d", videoList[idx].videoviewIndex);
  225. json_object_object_add(imageview, "videoviewIndex", json_object_new_string(buf));
  226. sprintf(buf, "%d", videoList[idx].videosrc_addr);
  227. json_object_object_add(imageview, "videoSrc_addr", json_object_new_string(buf));
  228. sprintf(buf, "%d", videoList[idx].layout_x);
  229. json_object_object_add(imageview, "layout_x", json_object_new_string(buf));
  230. sprintf(buf, "%d", videoList[idx].layout_y);
  231. json_object_object_add(imageview, "layout_y", json_object_new_string(buf));
  232. sprintf(buf, "%d", videoList[idx].width);
  233. json_object_object_add(imageview, "width", json_object_new_string(buf));
  234. sprintf(buf, "%d", videoList[idx].height);
  235. json_object_object_add(imageview, "height", json_object_new_string(buf));
  236. json_object_array_add(payload, imageview);
  237. }
  238. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  239. json_object_put(payload);
  240. return publish_data(mosq, "client/layout/videoview", outputStr);
  241. }
  242. /**
  243. *
  244. * @param mosq
  245. */
  246. int publish_qrcodeimage_add(struct mosquitto *mosq, QrCode_List *qrCodeList, uint8_t listCount)
  247. {
  248. json_object *payload = json_object_new_array();
  249. char outputStr[2048]={0};
  250. for(int idx=0;idx<listCount;idx++)
  251. {
  252. json_object *qrcodeImage = json_object_new_object();
  253. char buf[32];
  254. sprintf(buf, "%d", qrCodeList[idx].qrCodeIndex);
  255. json_object_object_add(qrcodeImage, "qrCodeIndex", json_object_new_string(buf));
  256. sprintf(buf, "%s", qrCodeList[idx].qrCodeContent);
  257. json_object_object_add(qrcodeImage, "qrCodeContent", json_object_new_string(buf));
  258. sprintf(buf, "%s", qrCodeList[idx].errorCorrection);
  259. json_object_object_add(qrcodeImage, "errorCorrection", json_object_new_string(buf));
  260. sprintf(buf, "%d", qrCodeList[idx].layout_x);
  261. json_object_object_add(qrcodeImage, "layout_x", json_object_new_string(buf));
  262. sprintf(buf, "%d", qrCodeList[idx].layout_y);
  263. json_object_object_add(qrcodeImage, "layout_y", json_object_new_string(buf));
  264. sprintf(buf, "%d", qrCodeList[idx].width);
  265. json_object_object_add(qrcodeImage, "width", json_object_new_string(buf));
  266. sprintf(buf, "%d", qrCodeList[idx].height);
  267. json_object_object_add(qrcodeImage, "height", json_object_new_string(buf));
  268. json_object_array_add(payload, qrcodeImage);
  269. }
  270. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  271. json_object_put(payload);
  272. return publish_data(mosq, "client/layout/qrcodeview", outputStr);
  273. }
  274. /**
  275. *
  276. * @param mosq
  277. */
  278. int publish_view_remove(struct mosquitto *mosq, Text_List *textList, uint8_t textListCount,
  279. Image_List *imageList, uint8_t imageListCount,
  280. Video_List *videoList, uint8_t videoListCount,
  281. QrCode_List *qrCodeList, uint8_t qrCodeListCount)
  282. {
  283. json_object *payload = json_object_new_object();
  284. json_object *text = json_object_new_array();
  285. json_object *image = json_object_new_array();
  286. json_object *video = json_object_new_array();
  287. json_object *qrCode = json_object_new_array();
  288. char outputStr[2048]={0};
  289. // Remove text view
  290. for(int idx=0;idx<textListCount;idx++)
  291. {
  292. json_object *textview = json_object_new_object();
  293. char buf[8];
  294. sprintf(buf, "%d", textList[idx].textviewIndex);
  295. json_object_object_add(textview, "textviewIndex", json_object_new_string(buf));
  296. json_object_array_add(text, textview);
  297. }
  298. json_object_object_add(payload, "rm_textview_list", text);
  299. // Remove image view
  300. for(int idx=0;idx<imageListCount;idx++)
  301. {
  302. json_object *imageview = json_object_new_object();
  303. char buf[8];
  304. sprintf(buf, "%d", imageList[idx].imageviewIndex);
  305. json_object_object_add(imageview, "imageviewIndex", json_object_new_string(buf));
  306. json_object_array_add(image, imageview);
  307. }
  308. json_object_object_add(payload, "rm_imageview_list", image);
  309. // Remove video view
  310. for(int idx=0;idx<videoListCount;idx++)
  311. {
  312. json_object *videoview = json_object_new_object();
  313. char buf[8];
  314. sprintf(buf, "%d", videoList[idx].videoviewIndex);
  315. json_object_object_add(videoview, "videoviewIndex", json_object_new_string(buf));
  316. json_object_array_add(video, videoview);
  317. }
  318. json_object_object_add(payload, "rm_videoview_list", video);
  319. // Remove qrCode view
  320. for(int idx=0;idx<qrCodeListCount;idx++)
  321. {
  322. json_object *qrCodeview = json_object_new_object();
  323. char buf[8];
  324. sprintf(buf, "%d", qrCodeList[idx].qrCodeIndex);
  325. json_object_object_add(qrCodeview, "qrCodeIndex", json_object_new_string(buf));
  326. json_object_array_add(qrCode, qrCodeview);
  327. }
  328. json_object_object_add(payload, "rm_qrcode_list", qrCode);
  329. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  330. json_object_put(payload);
  331. return publish_data(mosq, "client/layout/remove", outputStr);
  332. }
  333. /**
  334. *
  335. * @param mosq
  336. */
  337. int publish_clear_screen(struct mosquitto *mosq)
  338. {
  339. json_object *payload = json_object_new_object();
  340. char outputStr[2048]={0};
  341. json_object_object_add(payload, "task_name", json_object_new_string("clear_screen"));
  342. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  343. json_object_put(payload);
  344. return publish_data(mosq, "client/command", outputStr);
  345. }