LcmCommandDriver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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_YES);
  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. json_object *payload = json_object_new_object();
  75. char outputStr[2048]={0};
  76. json_object_object_add(payload, "task_name", json_object_new_string("power_saving"));
  77. json_object_object_add(payload, "mode", json_object_new_string(isSleep?"1":"0"));
  78. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  79. json_object_put(payload);
  80. return publish_data(mosq, "client/command", outputStr);
  81. }
  82. /**
  83. *
  84. * @param mosq
  85. */
  86. int publish_back_dimming(struct mosquitto *mosq, uint8_t lightLevel)
  87. {
  88. json_object *payload = json_object_new_object();
  89. char outputStr[2048]={0};
  90. char buf[8];
  91. json_object_object_add(payload, "task_name", json_object_new_string("backlight"));
  92. sprintf(buf, "%d", lightLevel);
  93. json_object_object_add(payload, "brightness", json_object_new_string(buf));
  94. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  95. json_object_put(payload);
  96. return publish_data(mosq, "client/command", outputStr);
  97. }
  98. /**
  99. *
  100. * @param mosq
  101. */
  102. int publish_audio_volume(struct mosquitto *mosq, uint8_t volume)
  103. {
  104. json_object *payload = json_object_new_object();
  105. char outputStr[2048]={0};
  106. char buf[8];
  107. json_object_object_add(payload, "task_name", json_object_new_string("audio_volume"));
  108. sprintf(buf, "%d", volume);
  109. json_object_object_add(payload, "volume", json_object_new_string(buf));
  110. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  111. json_object_put(payload);
  112. return publish_data(mosq, "client/command", outputStr);
  113. }
  114. /**
  115. *
  116. * @param mosq
  117. */
  118. int publish_trigger_report_status(struct mosquitto *mosq)
  119. {
  120. json_object *payload = json_object_new_object();
  121. char outputStr[2048]={0};
  122. json_object_object_add(payload, "task_name", json_object_new_string("report_status"));
  123. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  124. json_object_put(payload);
  125. return publish_data(mosq, "client/command", outputStr);
  126. }
  127. /**
  128. *
  129. * @param mosq
  130. */
  131. int publish_timesync(struct mosquitto *mosq)
  132. {
  133. char outputStr[64]={0};
  134. time_t CurrentTime;
  135. struct tm *tm;
  136. struct timeval tv;
  137. CurrentTime = time(NULL);
  138. tm=localtime(&CurrentTime);
  139. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  140. 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);
  141. return publish_data(mosq, "client/timesync", outputStr);
  142. }
  143. /**
  144. *
  145. * @param mosq
  146. */
  147. int publish_textview_add(struct mosquitto *mosq, Text_List *textList, uint8_t listCount)
  148. {
  149. json_object *payload = json_object_new_array();
  150. char outputStr[2048]={0};
  151. for(int idx=0;idx<listCount;idx++)
  152. {
  153. json_object *textview = json_object_new_object();
  154. char buf[32];
  155. sprintf(buf, "%d", textList[idx].textviewIndex);
  156. json_object_object_add(textview, "textviewIndex", json_object_new_string(buf));
  157. json_object_object_add(textview, "textString", json_object_new_string(textList[idx].textString));
  158. json_object_object_add(textview, "textFont", json_object_new_string(textList[idx].textFont));
  159. json_object_object_add(textview, "textStyle", json_object_new_string(textList[idx].textStyle));
  160. sprintf(buf, "%d", textList[idx].textSize);
  161. json_object_object_add(textview, "textSize", json_object_new_string(buf));
  162. sprintf(buf, "%d", textList[idx].layout_x);
  163. json_object_object_add(textview, "layout_x", json_object_new_string(buf));
  164. sprintf(buf, "%d", textList[idx].layout_y);
  165. json_object_object_add(textview, "layout_y", json_object_new_string(buf));
  166. json_object_array_add(payload, textview);
  167. }
  168. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  169. json_object_put(payload);
  170. return publish_data(mosq, "client/layout/textview", outputStr);
  171. }
  172. /**
  173. *
  174. * @param mosq
  175. */
  176. int publish_imageview_add(struct mosquitto *mosq, Image_List *imageList, uint8_t listCount)
  177. {
  178. json_object *payload = json_object_new_array();
  179. char outputStr[2048]={0};
  180. for(int idx=0;idx<listCount;idx++)
  181. {
  182. json_object *imageview = json_object_new_object();
  183. char buf[32];
  184. sprintf(buf, "%d", imageList[idx].imageviewIndex);
  185. json_object_object_add(imageview, "imageviewIndex", json_object_new_string(buf));
  186. sprintf(buf, "%d", imageList[idx].imgsrc_addr);
  187. json_object_object_add(imageview, "imgsrc_addr", json_object_new_string(buf));
  188. sprintf(buf, "%d", imageList[idx].layout_x);
  189. json_object_object_add(imageview, "layout_x", json_object_new_string(buf));
  190. sprintf(buf, "%d", imageList[idx].layout_y);
  191. json_object_object_add(imageview, "layout_y", json_object_new_string(buf));
  192. sprintf(buf, "%d", imageList[idx].width);
  193. json_object_object_add(imageview, "width", json_object_new_string(buf));
  194. sprintf(buf, "%d", imageList[idx].height);
  195. json_object_object_add(imageview, "height", json_object_new_string(buf));
  196. json_object_array_add(payload, imageview);
  197. }
  198. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  199. json_object_put(payload);
  200. return publish_data(mosq, "client/layout/imageview", outputStr);
  201. }
  202. /**
  203. *
  204. * @param mosq
  205. */
  206. int publish_videoview_add(struct mosquitto *mosq, Video_List *videoList, uint8_t listCount)
  207. {
  208. json_object *payload = json_object_new_array();
  209. char outputStr[2048]={0};
  210. for(int idx=0;idx<listCount;idx++)
  211. {
  212. json_object *imageview = json_object_new_object();
  213. char buf[32];
  214. sprintf(buf, "%d", videoList[idx].videoviewIndex);
  215. json_object_object_add(imageview, "videoviewIndex", json_object_new_string(buf));
  216. sprintf(buf, "%d", videoList[idx].videosrc_addr);
  217. json_object_object_add(imageview, "videosrc_addr", json_object_new_string(buf));
  218. sprintf(buf, "%d", videoList[idx].layout_x);
  219. json_object_object_add(imageview, "layout_x", json_object_new_string(buf));
  220. sprintf(buf, "%d", videoList[idx].layout_y);
  221. json_object_object_add(imageview, "layout_y", json_object_new_string(buf));
  222. sprintf(buf, "%d", videoList[idx].width);
  223. json_object_object_add(imageview, "width", json_object_new_string(buf));
  224. sprintf(buf, "%d", videoList[idx].height);
  225. json_object_object_add(imageview, "height", json_object_new_string(buf));
  226. json_object_array_add(payload, imageview);
  227. }
  228. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  229. json_object_put(payload);
  230. return publish_data(mosq, "client/layout/videoview", outputStr);
  231. }
  232. /**
  233. *
  234. * @param mosq
  235. */
  236. int publish_view_remove(struct mosquitto *mosq, Text_List *textList, uint8_t textListCount, Image_List *imageList, uint8_t imageListCount, Video_List *videoList, uint8_t videoListCount)
  237. {
  238. json_object *payload = json_object_new_object();
  239. json_object *text = json_object_new_array();
  240. json_object *image = json_object_new_array();
  241. json_object *video = json_object_new_array();
  242. char outputStr[2048]={0};
  243. // Remove text view
  244. for(int idx=0;idx<textListCount;idx++)
  245. {
  246. json_object *textview = json_object_new_object();
  247. char buf[8];
  248. sprintf(buf, "%d", textList[idx].textviewIndex);
  249. json_object_object_add(textview, "textviewIndex", json_object_new_string(buf));
  250. json_object_array_add(text, textview);
  251. }
  252. json_object_object_add(payload, "rm_textview_list", text);
  253. // Remove image view
  254. for(int idx=0;idx<imageListCount;idx++)
  255. {
  256. json_object *imageview = json_object_new_object();
  257. char buf[8];
  258. sprintf(buf, "%d", imageList[idx].imageviewIndex);
  259. json_object_object_add(imageview, "imageviewIndex", json_object_new_string(buf));
  260. json_object_array_add(image, imageview);
  261. }
  262. json_object_object_add(payload, "rm_imageview_list", image);
  263. // Remove video view
  264. for(int idx=0;idx<videoListCount;idx++)
  265. {
  266. json_object *videoview = json_object_new_object();
  267. char buf[8];
  268. sprintf(buf, "%d", videoList[idx].videoviewIndex);
  269. json_object_object_add(videoview, "videoviewIndex", json_object_new_string(buf));
  270. json_object_array_add(video, videoview);
  271. }
  272. json_object_object_add(payload, "rm_videoview_list", video);
  273. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  274. json_object_put(payload);
  275. return publish_data(mosq, "client/layout/remove", outputStr);
  276. }
  277. /**
  278. *
  279. * @param mosq
  280. */
  281. int publish_clear_screen(struct mosquitto *mosq)
  282. {
  283. json_object *payload = json_object_new_object();
  284. char outputStr[2048]={0};
  285. json_object_object_add(payload, "task_name", json_object_new_string("clear_screen"));
  286. sprintf(outputStr, "%s", json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN));
  287. json_object_put(payload);
  288. return publish_data(mosq, "client/command", outputStr);
  289. }