apps.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. #include "common.h"
  2. #include <arpa/inet.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <json-c/json.h>
  6. #include <json-c/json_object.h>
  7. #include <json-c/json_util.h>
  8. #include <mosquitto.h>
  9. #include <netdb.h>
  10. #include <netinet/in.h>
  11. #include <netinet/ip.h>
  12. #include <netinet/tcp.h>
  13. #include <pthread.h>
  14. #include <signal.h>
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/socket.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <termios.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #define MAX_VCOM 4
  26. #define ETH_0_NAME "eth0"
  27. #define ETH_1_NAME "eth1"
  28. #define GET_LAN_IP "ifconfig eth0 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
  29. #define GET_LAN_NETMASK "ifconfig eth0 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
  30. #define GET_LAN_GATEWAY "ifconfig eth0 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
  31. #define GET_LAN_MACADDR "ifconfig eth0 | grep 'HWaddr' | awk -F' ' '{print $5}'"
  32. #define GET_LAN_STATE "cat /sys/class/net/eth0/operstate"
  33. #define GET_WAN_IP "ifconfig eth1 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
  34. #define GET_WAN_NETMASK "ifconfig eth1 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
  35. #define GET_WAN_GATEWAY "ifconfig eth1 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
  36. #define GET_WAN_MACADDR "ifconfig eth1 | grep 'HWaddr' | awk -F' ' '{print $5}'"
  37. #define GET_WAN_STATE "cat /sys/class/net/eth1/operstate"
  38. #define GET_LTE_IP "ifconfig usb0 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
  39. #define GET_LTE_NETMASK "ifconfig usb0 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
  40. #define GET_LTE_GATEWAY "ifconfig usb0 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
  41. #define GET_LTE_MACADDR "ifconfig usb0 | grep 'HWaddr' | awk -F' ' '{print $5}'"
  42. #define GET_LTE_STATE "cat /sys/class/net/usb0/operstate"
  43. #define GET_WLAN_IP "ifconfig wlan0 | grep 'inet addr' | awk -F'[ :]+' '{print $4}'"
  44. #define GET_WLAN_NETMASK "ifconfig wlan0 | grep 'inet addr' | awk -F'[ :]+' '{print $8}'"
  45. #define GET_WLAN_GATEWAY "ifconfig wlan0 | grep 'inet addr' | awk -F'[ :]+' '{print $6}'"
  46. #define GET_WLAN_MACADDR "ifconfig wlan0 | grep 'HWaddr' | awk -F' ' '{print $5}'"
  47. #define GET_WLAN_STATE "cat /sys/class/net/wlan0/operstate"
  48. #define GET_TOTAL_MEM "free -m | grep 'Mem:' | awk -F' ' '{print $2}'"
  49. #define GET_USED_MEM "free -m | grep 'Mem:' | awk -F' ' '{print $3}'"
  50. #define GET_TOTAL_FLASH "4096"
  51. #define GET_USED_FLASH "362"
  52. #define GET_NOWTIME "date +'%Y-%m-%d %H:%M.%S'"
  53. int vcom_fd[MAX_VCOM];
  54. struct mosquitto* mosq_up_mqtt[MAX_VCOM];
  55. struct mosquitto* mosq_dw_mqtt[MAX_VCOM];
  56. volatile bool exit_sig = false;
  57. volatile bool quit_sig = false;
  58. int net_sock = 0;
  59. struct json_object* root_obj = NULL;
  60. pthread_mutex_t mx_db = PTHREAD_MUTEX_INITIALIZER;
  61. pthread_mutex_t root_lock;
  62. struct mosquitto* mosq_http;
  63. static uint16_t CRC16(const uint8_t* p_data, uint32_t len, uint16_t* p_crc)
  64. {
  65. uint32_t i;
  66. uint16_t crc = 0xffff;
  67. if (NULL == p_data || NULL == p_crc || 0 == len) {
  68. return -1;
  69. }
  70. for (i = 0; i < len; i++) {
  71. crc = (unsigned char)(crc >> 8) | (crc << 8);
  72. crc ^= p_data[i];
  73. crc ^= (unsigned char)(crc & 0xff) >> 4;
  74. crc ^= (crc << 8) << 4;
  75. crc ^= ((crc & 0xff) << 4) << 1;
  76. }
  77. *p_crc = crc;
  78. return 0;
  79. }
  80. void exec_cmd(char* cmd, char* buf)
  81. {
  82. FILE* fp = NULL;
  83. if ((fp = popen(cmd, "r")) == NULL) {
  84. printf("error: popen failed!\r\n");
  85. exit(1);
  86. }
  87. while (fgets(buf, 199, fp) != NULL) {
  88. if (buf[strlen(buf) - 1] == '\n')
  89. buf[strlen(buf) - 1] = '\0';
  90. }
  91. pclose(fp);
  92. }
  93. static void sig_handler(int sigio)
  94. {
  95. if (sigio == SIGQUIT) {
  96. quit_sig = true;
  97. } else if ((sigio == SIGINT) || (sigio == SIGTERM)) {
  98. exit_sig = true;
  99. }
  100. return;
  101. }
  102. /*
  103. modbus ---> json
  104. {
  105. "reg_0001":23,
  106. "reg_0002":24
  107. }
  108. */
  109. int dwlink_packet_process(uint8_t* device_id_str,
  110. uint8_t* dw_buffer,
  111. uint8_t* dw_buffer_len,
  112. uint8_t* attr_addr_str,
  113. uint8_t* attr_value_str)
  114. {
  115. struct json_object* dw_obj = NULL;
  116. dw_obj = json_object_new_object();
  117. if (dw_obj == NULL) {
  118. printf("dw_obj is null\r\n");
  119. return NULL;
  120. }
  121. json_object_object_add(dw_obj, attr_addr_str, json_object_new_string(attr_value_str));
  122. if (strlen(json_object_to_json_string(dw_obj)) < 255) {
  123. memcpy(dw_buffer, json_object_to_json_string(dw_obj), strlen(json_object_to_json_string(dw_obj)));
  124. *dw_buffer_len = strlen(json_object_to_json_string(dw_obj));
  125. }
  126. json_object_put(dw_obj);
  127. }
  128. void config_init()
  129. {
  130. root_obj = json_object_from_file("./app.json");
  131. if (root_obj == NULL) {
  132. return 0;
  133. }
  134. }
  135. void mosq_http_connect_callback(struct mosquitto* mosq, void* obj, int rc)
  136. {
  137. printf("http_json_connect_callback set\r\n");
  138. }
  139. void mosq_http_disconnect_callback(struct mosquitto* mosq, void* obj, int result)
  140. {
  141. printf("http_json_disconnect_callback set\r\n");
  142. }
  143. struct json_object* get_system_json()
  144. {
  145. struct json_object *rsp_obj = NULL, *system_obj = NULL;
  146. rsp_obj = json_object_new_object();
  147. if (rsp_obj == NULL) {
  148. printf("rsp_obj is null\r\n");
  149. return NULL;
  150. }
  151. json_object_object_get_ex(root_obj, "system", &system_obj);
  152. json_object_object_add(rsp_obj, "system", system_obj);
  153. json_object_get(system_obj);
  154. json_object_object_add(rsp_obj, "state", json_object_new_string("success"));
  155. return rsp_obj;
  156. }
  157. struct json_object* get_platform_json()
  158. {
  159. struct json_object *rsp_obj = NULL, *platform_obj = NULL;
  160. rsp_obj = json_object_new_object();
  161. if (rsp_obj == NULL) {
  162. printf("rsp_obj is null\r\n");
  163. return NULL;
  164. }
  165. json_object_object_get_ex(root_obj, "platform", &platform_obj);
  166. json_object_object_add(rsp_obj, "platform", platform_obj);
  167. json_object_get(platform_obj);
  168. json_object_object_add(rsp_obj, "state", json_object_new_string("success"));
  169. return rsp_obj;
  170. }
  171. struct json_object* get_user_json()
  172. {
  173. struct json_object *rsp_obj = NULL, *user_obj = NULL;
  174. rsp_obj = json_object_new_object();
  175. if (rsp_obj == NULL) {
  176. printf("rsp_obj is null\r\n");
  177. return NULL;
  178. }
  179. json_object_object_get_ex(root_obj, "user", &user_obj);
  180. json_object_object_add(rsp_obj, "user", user_obj);
  181. json_object_get(user_obj);
  182. json_object_object_add(rsp_obj, "state", json_object_new_string("success"));
  183. return rsp_obj;
  184. }
  185. struct json_object* get_devices_json()
  186. {
  187. struct json_object* rsp_obj = NULL;
  188. select_all_device(&rsp_obj);
  189. return rsp_obj;
  190. }
  191. struct json_object* set_devices_json(char* payload)
  192. {
  193. struct json_object *payload_obj = NULL, *item_obj = NULL, *devices_obj = NULL, *device_id_obj = NULL,
  194. *device_name_obj = NULL, *device_addr_obj = NULL, *application_id_obj = NULL;
  195. char *device_id_str = NULL, *device_name_str = NULL, *device_addr_str = NULL, *application_id_str = NULL;
  196. int len = 0, index = 0;
  197. payload_obj = json_tokener_parse(payload);
  198. if (payload_obj == NULL) {
  199. printf("payload is null\r\n");
  200. return NULL;
  201. }
  202. json_object_object_get_ex(payload_obj, "devices", &devices_obj);
  203. len = json_object_array_length(devices_obj);
  204. for (index = 0; index < len; index++) {
  205. item_obj = json_object_array_get_idx(devices_obj, index);
  206. json_object_object_get_ex(item_obj, "device_id", &device_id_obj);
  207. device_id_str = json_object_get_string(device_id_obj);
  208. json_object_object_get_ex(item_obj, "device_name", &device_name_obj);
  209. device_name_str = json_object_get_string(device_name_obj);
  210. json_object_object_get_ex(item_obj, "device_addr", &device_addr_obj);
  211. device_addr_str = json_object_get_string(device_addr_obj);
  212. json_object_object_get_ex(item_obj, "application_id", &application_id_obj);
  213. application_id_str = json_object_get_string(application_id_obj);
  214. pthread_mutex_lock(&mx_db);
  215. update_device_by_device_id(tolower(device_id_str), tolower(device_name_str), atoi(device_addr_str),
  216. tolower(application_id_str));
  217. pthread_mutex_unlock(&mx_db);
  218. }
  219. json_object_object_add(payload_obj, "state", json_object_new_string("success"));
  220. return payload_obj;
  221. }
  222. struct json_object* set_platform_json(char* payload)
  223. {
  224. struct json_object *payload_obj = NULL, *platform_obj = NULL;
  225. payload_obj = json_tokener_parse(payload);
  226. if (payload_obj == NULL) {
  227. printf("payload is null\r\n");
  228. return NULL;
  229. }
  230. json_object_object_get_ex(payload_obj, "platform", &platform_obj);
  231. json_object_object_add(root_obj, "platform", platform_obj);
  232. json_object_get(platform_obj);
  233. json_object_to_file_ext("./app.json", root_obj, JSON_C_TO_STRING_PRETTY);
  234. sync();
  235. json_object_object_add(payload_obj, "state", json_object_new_string("success"));
  236. return payload_obj;
  237. }
  238. struct json_object* del_devices_json(char* payload)
  239. {
  240. struct json_object *payload_obj = NULL, *item_obj = NULL, *devices_obj = NULL, *device_id_obj = NULL,
  241. *device_name_obj = NULL, *device_addr_obj = NULL, *module_id_obj = NULL;
  242. char *device_id_str = NULL, *device_name_str = NULL, *device_addr_str = NULL, *module_id_str = NULL;
  243. int len = 0, index = 0;
  244. payload_obj = json_tokener_parse(payload);
  245. if (payload_obj == NULL) {
  246. printf("payload is null\r\n");
  247. return NULL;
  248. }
  249. json_object_object_get_ex(payload_obj, "devices", &devices_obj);
  250. len = json_object_array_length(devices_obj);
  251. for (index = 0; index < len; index++) {
  252. item_obj = json_object_array_get_idx(devices_obj, index);
  253. json_object_object_get_ex(item_obj, "device_id", &device_id_obj);
  254. device_id_str = json_object_get_string(device_id_obj);
  255. pthread_mutex_lock(&mx_db);
  256. delete_device_by_device_id(device_id_str);
  257. pthread_mutex_unlock(&mx_db);
  258. }
  259. json_object_object_add(payload_obj, "state", json_object_new_string("success"));
  260. return payload_obj;
  261. }
  262. struct json_object* add_devices_json(char* payload)
  263. {
  264. struct json_object *payload_obj = NULL, *item_obj = NULL, *devices_obj = NULL, *device_id_obj = NULL,
  265. *device_name_obj = NULL, *device_addr_obj = NULL, *application_id_obj = NULL;
  266. char *device_id_str = NULL, *device_name_str = NULL, *device_addr_str = NULL, *application_id_str = NULL;
  267. int len = 0, index = 0;
  268. int ret = 0;
  269. payload_obj = json_tokener_parse(payload);
  270. if (payload_obj == NULL) {
  271. printf("payload is null\r\n");
  272. return NULL;
  273. }
  274. json_object_object_get_ex(payload_obj, "devices", &devices_obj);
  275. len = json_object_array_length(devices_obj);
  276. for (index = 0; index < len; index++) {
  277. item_obj = json_object_array_get_idx(devices_obj, index);
  278. json_object_object_get_ex(item_obj, "device_id", &device_id_obj);
  279. device_id_str = json_object_get_string(device_id_obj);
  280. json_object_object_get_ex(item_obj, "device_name", &device_name_obj);
  281. device_name_str = json_object_get_string(device_name_obj);
  282. json_object_object_get_ex(item_obj, "device_addr", &device_addr_obj);
  283. device_addr_str = json_object_get_string(device_addr_obj);
  284. json_object_object_get_ex(item_obj, "application_id", &application_id_obj);
  285. application_id_str = json_object_get_string(application_id_obj);
  286. pthread_mutex_lock(&mx_db);
  287. ret = insert_device_item(tolower(device_id_str), tolower(device_name_str), atoi(device_addr_str),
  288. tolower(application_id_str));
  289. pthread_mutex_unlock(&mx_db);
  290. }
  291. if (ret == 0)
  292. json_object_object_add(payload_obj, "state", json_object_new_string("success"));
  293. else
  294. json_object_object_add(payload_obj, "state", json_object_new_string("fail"));
  295. return payload_obj;
  296. }
  297. // req:get/devices/none/http
  298. // rsp:ack/get/device/none/http
  299. void mosq_http_json_message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_message* msg)
  300. {
  301. struct json_object* rsp_obj = NULL;
  302. char** topics;
  303. int topic_count;
  304. char pub_topic[200];
  305. mosquitto_sub_topic_tokenise(msg->topic, &topics, &topic_count);
  306. if (strncmp(topics[0], "get", STR_MAX_LEN(topics[0], "get")) == 0) {
  307. if (strncmp(topics[1], "system", STR_MAX_LEN(topics[1], "system")) == 0) {
  308. pthread_mutex_lock(&root_lock);
  309. rsp_obj = get_system_json();
  310. pthread_mutex_unlock(&root_lock);
  311. if (rsp_obj != NULL) {
  312. printf("get system rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  313. }
  314. } else if (strncmp(topics[1], "platform", STR_MAX_LEN(topics[1], "platform")) == 0) {
  315. pthread_mutex_lock(&root_lock);
  316. rsp_obj = get_platform_json();
  317. pthread_mutex_unlock(&root_lock);
  318. if (rsp_obj != NULL) {
  319. printf("get platform rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  320. }
  321. } else if (strncmp(topics[1], "user", STR_MAX_LEN(topics[1], "user")) == 0) {
  322. pthread_mutex_lock(&root_lock);
  323. rsp_obj = get_user_json();
  324. pthread_mutex_unlock(&root_lock);
  325. if (rsp_obj != NULL) {
  326. printf("get user rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  327. }
  328. } else if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
  329. pthread_mutex_lock(&root_lock);
  330. rsp_obj = get_devices_json();
  331. pthread_mutex_unlock(&root_lock);
  332. if (rsp_obj != NULL) {
  333. printf("get devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  334. }
  335. }
  336. } else if (strncmp(topics[0], "set", STR_MAX_LEN(topics[0], "set")) == 0) {
  337. if (strncmp(topics[1], "platform", STR_MAX_LEN(topics[1], "platform")) == 0) {
  338. pthread_mutex_lock(&root_lock);
  339. rsp_obj = set_platform_json(msg->payload);
  340. pthread_mutex_unlock(&root_lock);
  341. if (rsp_obj != NULL) {
  342. printf("set platform rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  343. }
  344. } else if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
  345. pthread_mutex_lock(&root_lock);
  346. rsp_obj = set_devices_json(msg->payload);
  347. pthread_mutex_unlock(&root_lock);
  348. if (rsp_obj != NULL) {
  349. printf("set devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  350. }
  351. }
  352. } else if (strncmp(topics[0], "del", STR_MAX_LEN(topics[0], "del")) == 0) {
  353. if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
  354. pthread_mutex_lock(&root_lock);
  355. rsp_obj = del_devices_json(msg->payload);
  356. pthread_mutex_unlock(&root_lock);
  357. if (rsp_obj != NULL) {
  358. printf("del devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  359. }
  360. }
  361. } else if (strncmp(topics[0], "add", STR_MAX_LEN(topics[0], "add")) == 0) {
  362. if (strncmp(topics[1], "devices", STR_MAX_LEN(topics[1], "devices")) == 0) {
  363. pthread_mutex_lock(&root_lock);
  364. rsp_obj = add_devices_json(msg->payload);
  365. pthread_mutex_unlock(&root_lock);
  366. if (rsp_obj != NULL) {
  367. printf("add devices rsp_obj:%s\r\n", json_object_to_json_string(rsp_obj));
  368. }
  369. }
  370. }
  371. if (rsp_obj != NULL) {
  372. sprintf(pub_topic, "ack/%s/%s/%s/%s", topics[0], topics[1], topics[2], topics[3]);
  373. mosquitto_publish(mosq, NULL, pub_topic, strlen(json_object_to_json_string(rsp_obj)),
  374. json_object_to_json_string(rsp_obj), 1, false);
  375. json_object_put(rsp_obj);
  376. }
  377. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  378. printf("mosq_http_json_message_callback\r\n");
  379. }
  380. void mosq_http_json_subscribe_callback(struct mosquitto* mosq,
  381. void* obj,
  382. int mid,
  383. int qos_count,
  384. const int* granted_qos)
  385. {
  386. printf("http_json_subscribe_callback set\r\n");
  387. }
  388. // req:get/devices/none/http
  389. // rsp:ack/get/device/none/http
  390. void http_json_process_thread(void)
  391. {
  392. char pub_topic[200];
  393. char id[50];
  394. snprintf(id, 50, "http_json_%d", getpid());
  395. mosq_http = mosquitto_new(id, true, NULL);
  396. mosquitto_connect_callback_set(mosq_http, mosq_http_connect_callback);
  397. mosquitto_disconnect_callback_set(mosq_http, mosq_http_disconnect_callback);
  398. mosquitto_message_callback_set(mosq_http, mosq_http_json_message_callback);
  399. mosquitto_subscribe_callback_set(mosq_http, mosq_http_json_subscribe_callback);
  400. mosquitto_connect(mosq_http, "localhost", 1883, 600);
  401. // mosquitto_username_pw_set(mosq_http,"leo_mqtt", "leo_mqtt@");
  402. mosquitto_subscribe(mosq_http, NULL, "+/+/+/http", 1);
  403. while (!exit_sig && !quit_sig) {
  404. if (mosquitto_loop(mosq_http, 100, 1)) {
  405. mosquitto_reconnect(mosq_http);
  406. sleep(10);
  407. }
  408. usleep(500000);
  409. }
  410. printf("http_json_process_thread exit\r\n");
  411. }
  412. int get_system_status()
  413. {
  414. char buf[200];
  415. struct json_object* system_obj = NULL;
  416. json_object_object_get_ex(root_obj, "system", &system_obj);
  417. memset(buf, '\0', 200);
  418. exec_cmd(GET_WAN_MACADDR, buf);
  419. // printf("WAN-ADDR:%s\r\n",buf);
  420. json_object_object_add(system_obj, "wan_mac", json_object_new_string(buf));
  421. memset(buf, '\0', 200);
  422. exec_cmd(GET_WAN_STATE, buf);
  423. // printf("WAN-STTE:%s\r\n",buf);
  424. if (strncmp(buf, "up", STR_MAX_LEN("up", buf)) == 0) {
  425. json_object_object_add(system_obj, "ethernet_wan", json_object_new_string("连接"));
  426. } else {
  427. json_object_object_add(system_obj, "ethernet_wan", json_object_new_string("断开"));
  428. }
  429. memset(buf, '\0', 200);
  430. exec_cmd(GET_LAN_MACADDR, buf);
  431. // printf("LAN-ADDR:%s\r\n",buf);
  432. json_object_object_add(system_obj, "lan_mac", json_object_new_string(buf));
  433. memset(buf, '\0', 200);
  434. exec_cmd(GET_LAN_STATE, buf);
  435. // printf("LAN-STATE:%s\r\n",buf);
  436. if (strncmp(buf, "up", STR_MAX_LEN("up", buf)) == 0) {
  437. json_object_object_add(system_obj, "ethernet_lan", json_object_new_string("连接"));
  438. } else {
  439. json_object_object_add(system_obj, "ethernet_lan", json_object_new_string("断开"));
  440. }
  441. memset(buf, '\0', 200);
  442. exec_cmd(GET_LTE_MACADDR, buf);
  443. // printf("LTE-ADDR:%s\r\n",buf);
  444. json_object_object_add(system_obj, "lte_mac", json_object_new_string(buf));
  445. memset(buf, '\0', 200);
  446. exec_cmd(GET_LTE_STATE, buf);
  447. // printf("LTE-STATE:%s\r\n",buf);
  448. if (strncmp(buf, "up", STR_MAX_LEN("up", buf)) == 0) {
  449. json_object_object_add(system_obj, "lte_status", json_object_new_string("连接"));
  450. } else {
  451. json_object_object_add(system_obj, "lte_status", json_object_new_string("断开"));
  452. }
  453. memset(buf, '\0', 200);
  454. exec_cmd(GET_TOTAL_MEM, buf);
  455. // printf("TOTAL-MEM:%s\r\n",buf);
  456. json_object_object_add(system_obj, "total_memory", json_object_new_string(buf));
  457. memset(buf, '\0', 200);
  458. exec_cmd(GET_USED_MEM, buf);
  459. // printf("USED-MEM:%s\r\n",buf);
  460. json_object_object_add(system_obj, "used_memory", json_object_new_string(buf));
  461. memset(buf, '\0', 200);
  462. // exec_cmd(GET_TOTAL_FLASH, buf);
  463. json_object_object_add(system_obj, "total_flash", json_object_new_string("4096"));
  464. memset(buf, '\0', 200);
  465. // exec_cmd(GET_USED_FLASH, buf);
  466. json_object_object_add(system_obj, "used_flash", json_object_new_string("361"));
  467. memset(buf, '\0', 200);
  468. exec_cmd(GET_NOWTIME, buf);
  469. json_object_object_add(system_obj, "boot_time", json_object_new_string(buf));
  470. }
  471. void mosq_up_mqtt_connect_callback(struct mosquitto* mosq, void* obj, int rc)
  472. {
  473. printf("mosq_up_mqtt_connect_callback set\r\n");
  474. }
  475. void mosq_up_mqtt_disconnect_callback(struct mosquitto* mosq, void* obj, int result)
  476. {
  477. printf("mosq_up_mqtt_disconnect_callback set\r\n");
  478. }
  479. void mosq_up_mqtt_message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_message* msg)
  480. {
  481. struct json_object *payload_obj = NULL, *devEUI_obj = NULL, *object_obj = NULL;
  482. char* devEUI_str = NULL;
  483. payload_obj = json_tokener_parse(msg->payload);
  484. if (payload_obj == NULL)
  485. goto _exit_0;
  486. json_object_object_get_ex(payload_obj, "devEUI", &devEUI_obj);
  487. devEUI_str = json_object_get_string(devEUI_obj);
  488. json_object_object_get_ex(payload_obj, "object", &object_obj);
  489. if (object_obj == NULL || object_obj == "") {
  490. goto _exit_0;
  491. }
  492. json_object_object_foreach(object_obj, key, val)
  493. {
  494. printf("key:%s --- value:%s \r\n", key, json_object_to_json_string(val));
  495. update_attrtab_item_by_device_id(devEUI_str, key, json_object_to_json_string(val));
  496. }
  497. json_object_put(payload_obj);
  498. _exit_0:
  499. printf("mqtt_process_message_callback\r\n");
  500. }
  501. void mosq_up_mqtt_subscribe_callback(struct mosquitto* mosq, void* obj, int mid, int qos_count, const int* granted_qos)
  502. {
  503. printf("mosq_up_mqtt_subscribe_callback set\r\n");
  504. }
  505. /*
  506. uplink topic: application/+/device/+/event/up
  507. {"applicationID":"1","applicationName":"leo","deviceName":"1000000000000001","deviceProfileName":"leo_otaa_c","deviceProfileID":"172bf053-caf5-49d1-8bdb-5d989aef9ec1","devEUI":"1000000000000001","rxInfo":[{"gatewayID":"aa555a0000000000","uplinkID":"312238b9-ffc3-4a4d-914d-e8ace8ffbae1","name":"AA555A0000000000","rssi":-93,"loRaSNR":8.2,"location":{"latitude":0,"longitude":0,"altitude":0}}],"txInfo":{"frequency":476100000,"dr":2},"adr":false,"fCnt":3,"fPort":2,"data":"MTIzNDU2","object":{"reg_0001":49,"reg_0002":50}}
  508. dwlink topic: application/+/device/+/command/down
  509. */
  510. void mqtt_modbus_uplink_process_thread(void* argc)
  511. {
  512. char sub_topic[200];
  513. char id[50];
  514. int mqtt_index = *((int*)argc);
  515. printf("mqtt_index_uplink:%d \r\n", mqtt_index);
  516. snprintf(id, 50, "mqtt_modbus_up_%d", mqtt_index);
  517. printf("mqtt_uplink_id:%s\r\n", id);
  518. mosq_up_mqtt[mqtt_index] = mosquitto_new(id, true, NULL);
  519. mosquitto_connect_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_connect_callback);
  520. mosquitto_disconnect_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_disconnect_callback);
  521. mosquitto_message_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_message_callback);
  522. mosquitto_subscribe_callback_set(mosq_up_mqtt[mqtt_index], mosq_up_mqtt_subscribe_callback);
  523. mosquitto_connect(mosq_up_mqtt[mqtt_index], "localhost", 1883, 600);
  524. sprintf(sub_topic, "application/%d/device/+/event/up", mqtt_index + 1);
  525. mosquitto_username_pw_set(mosq_up_mqtt[mqtt_index], "leo_mqtt", "leo_mqtt@");
  526. mosquitto_subscribe(mosq_up_mqtt[mqtt_index], NULL, sub_topic, 0);
  527. while (!exit_sig && !quit_sig) {
  528. if (mosquitto_loop(mosq_up_mqtt[mqtt_index], 100, 1)) {
  529. mosquitto_reconnect(mosq_up_mqtt[mqtt_index]);
  530. sleep(10);
  531. }
  532. usleep(500000);
  533. }
  534. mosquitto_disconnect(mosq_up_mqtt[mqtt_index]);
  535. mosquitto_destroy(mosq_up_mqtt[mqtt_index]);
  536. printf("mqtt_modbus_uplink_process_thread exit\r\n");
  537. }
  538. void mosq_dw_mqtt_connect_callback(struct mosquitto* mosq, void* obj, int rc)
  539. {
  540. printf("mosq_dw_mqtt_connect_callback set\r\n");
  541. }
  542. void mosq_dw_mqtt_disconnect_callback(struct mosquitto* mosq, void* obj, int result)
  543. {
  544. printf("mosq_dw_mqtt_disconnect_callback set\r\n");
  545. }
  546. void mosq_dw_mqtt_message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_message* msg)
  547. {
  548. printf("mosq_dw_mqtt_message_callback\r\n");
  549. }
  550. void mosq_dw_mqtt_subscribe_callback(struct mosquitto* mosq, void* obj, int mid, int qos_count, const int* granted_qos)
  551. {
  552. printf("mosq_dw_mqtt_subscribe_callback set\r\n");
  553. }
  554. /*
  555. dwlink topic: application/+/device/+/command/down
  556. */
  557. void mqtt_modbus_dwlink_process_thread(void* argc)
  558. {
  559. uint8_t rx_buffer[255] = {0};
  560. uint8_t tx_buffer[255] = {0};
  561. int ret = 0, i = 0, j = 0, rx_buffer_len = 0, tx_buffer_len = 0;
  562. char attr_addr_str[9], attr_value_str[5];
  563. uint16_t attr_value = 0, attr_num = 0, attr_addr = 0, crc = 0;
  564. uint8_t device_addr = 0, device_funcode = 0;
  565. uint8_t device_id[17];
  566. char pub_topic[200];
  567. char id[50];
  568. uint8_t dw_buffer[255];
  569. uint8_t dw_buffer_len = 0;
  570. int mqtt_index = *((int*)argc);
  571. printf("dwmqtt_index:%d\r\n", mqtt_index);
  572. snprintf(id, 50, "mqtt_modbus_dw_%d", mqtt_index);
  573. printf("dwid:%s\r\n", id);
  574. mosq_dw_mqtt[mqtt_index] = mosquitto_new(id, true, NULL);
  575. mosquitto_connect_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_connect_callback);
  576. mosquitto_disconnect_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_disconnect_callback);
  577. mosquitto_message_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_message_callback);
  578. mosquitto_subscribe_callback_set(mosq_dw_mqtt[mqtt_index], mosq_dw_mqtt_subscribe_callback);
  579. mosquitto_username_pw_set(mosq_dw_mqtt[mqtt_index], "leo_mqtt", "leo_mqtt@");
  580. mosquitto_connect(mosq_dw_mqtt[mqtt_index], "localhost", 1883, 600);
  581. while (!exit_sig && !quit_sig) {
  582. ret = read(vcom_fd[mqtt_index], rx_buffer, 255);
  583. if (ret > 0) {
  584. rx_buffer_len = ret;
  585. printf("info: rx modbus data -->");
  586. for (i = 0; i < rx_buffer_len; i++)
  587. printf("0x%x ", rx_buffer[i]);
  588. printf("\r\n");
  589. crc = App_Crc16(rx_buffer, rx_buffer_len - 2);
  590. if (crc != (*(rx_buffer + rx_buffer_len - 2) << 8 | *(rx_buffer + rx_buffer_len - 1))) {
  591. printf("rx modbus crc failed \r\n");
  592. tcflush(vcom_fd[mqtt_index], TCIOFLUSH);
  593. continue;
  594. }
  595. device_addr = rx_buffer[0];
  596. device_funcode = rx_buffer[1];
  597. tx_buffer[0] = device_addr;
  598. tx_buffer[1] = device_funcode;
  599. switch (device_funcode) {
  600. case 0x03: {
  601. attr_addr = rx_buffer[2] << 8 | rx_buffer[3];
  602. attr_num = rx_buffer[4] << 8 | rx_buffer[5];
  603. tx_buffer[2] = attr_num * 2;
  604. for (j = 0; j < attr_num; j++) {
  605. sprintf(attr_addr_str, "reg_%04x", (attr_addr + j));
  606. attr_addr_str[8] = '\0';
  607. pthread_mutex_lock(&mx_db);
  608. printf("attr_id:%s --device_addr:%d --appid:%d\r\n", attr_addr_str, device_addr,
  609. mqtt_index + 1);
  610. select_attr_by_device_addr(&attr_value, device_addr, attr_addr_str, mqtt_index + 1);
  611. pthread_mutex_unlock(&mx_db);
  612. tx_buffer[3 + 2 * j] = (attr_value >> 8 & 0xff);
  613. tx_buffer[3 + 2 * j + 1] = (attr_value >> 0 & 0xff);
  614. }
  615. crc = App_Crc16(tx_buffer, 3 + attr_num * 2);
  616. tx_buffer[3 + 2 * attr_num] = (crc >> 8 & 0xff);
  617. tx_buffer[3 + 2 * attr_num + 1] = (crc >> 0 & 0xff);
  618. tx_buffer_len = 3 + 2 * attr_num + 2;
  619. break;
  620. }
  621. case 0x06: {
  622. attr_addr = rx_buffer[2] << 8 | rx_buffer[3];
  623. attr_value = rx_buffer[4] << 8 | rx_buffer[5];
  624. sprintf(attr_addr_str, "reg_%04x", attr_addr);
  625. attr_addr_str[8] = '\0';
  626. sprintf(attr_value_str, "%d", attr_value);
  627. attr_value_str[4] = '\0';
  628. tx_buffer[2] = (attr_addr >> 8 & 0xff);
  629. tx_buffer[3] = (attr_addr >> 0 & 0xff);
  630. tx_buffer[4] = (attr_value >> 8 & 0xff);
  631. tx_buffer[5] = (attr_value >> 0 & 0xff);
  632. crc = App_Crc16(tx_buffer, 6);
  633. tx_buffer[6] = (crc >> 8 & 0xff);
  634. tx_buffer[7] = (crc >> 0 & 0xff);
  635. tx_buffer_len = 8;
  636. if (select_device_id_by_device_addr(device_addr, device_id, mqtt_index) == 1) {
  637. printf("attr_addr:%s --- attr-value:%s \r\n", attr_addr_str, attr_value_str);
  638. // update sqllite
  639. pthread_mutex_lock(&mx_db);
  640. update_tab_item_by_device_addr(device_addr, attr_addr_str, attr_value_str, mqtt_index);
  641. pthread_mutex_unlock(&mx_db);
  642. dwlink_packet_process(device_id, dw_buffer, &dw_buffer_len, attr_addr_str, attr_value_str);
  643. // publish dwlink/devices/id/mqtt
  644. sprintf(pub_topic, "application/%d/device/%s/command/down", mqtt_index + 1, device_id);
  645. mosquitto_publish(mosq_dw_mqtt[mqtt_index], NULL, pub_topic, dw_buffer_len, dw_buffer, 1,
  646. false);
  647. printf("publish:\r\n");
  648. for (i = 0; i < dw_buffer_len; i++)
  649. printf("%02x ", dw_buffer[i]);
  650. printf("\r\n");
  651. }
  652. break;
  653. }
  654. default: {
  655. tx_buffer[0] = device_addr;
  656. tx_buffer[1] = 0x83;
  657. tx_buffer[2] = 0x02;
  658. tx_buffer_len = 3;
  659. break;
  660. }
  661. }
  662. write(vcom_fd[mqtt_index], tx_buffer, tx_buffer_len);
  663. printf("info: tx modbus data -->");
  664. for (i = 0; i < tx_buffer_len; i++)
  665. printf("0x%x ", tx_buffer[i]);
  666. printf("\r\n");
  667. } else if (ret < 0) {
  668. continue;
  669. }
  670. if (mosquitto_loop(mosq_dw_mqtt[mqtt_index], 100, 1)) {
  671. mosquitto_reconnect(mosq_dw_mqtt[mqtt_index]);
  672. sleep(10);
  673. }
  674. usleep(500000);
  675. }
  676. mosquitto_disconnect(mosq_dw_mqtt[mqtt_index]);
  677. mosquitto_destroy(mosq_dw_mqtt[mqtt_index]);
  678. printf("mqtt_modbus_dwlink_process_thread exit\r\n");
  679. }
  680. int vcom_init()
  681. {
  682. char* dev[] = {"/dev/tty10", "/dev/tty12", "/dev/tty14", "/dev/tty16"};
  683. int i = 0;
  684. for (i = 0; i < MAX_VCOM; i++) {
  685. vcom_fd[i] = open(dev[i], O_RDWR | O_NOCTTY | O_NDELAY); // open("/dev/tttyS0",O_RDWR | O_NOCTTY); //O_NDELAY
  686. if (vcom_fd[i] == -1) {
  687. printf("can't open :%s \r\n", dev[i]);
  688. return -1;
  689. }
  690. }
  691. printf("vcom fd open success\r\n");
  692. return 1;
  693. }
  694. int main(int argc, char* argv[])
  695. {
  696. pthread_t mqtt_modbus_uplink_tid[MAX_VCOM], mqtt_modbus_dwlink_tid[MAX_VCOM], http_json_tid;
  697. int err = 0, i = 0;
  698. int thread_index[MAX_VCOM] = {0, 1, 2, 3};
  699. struct sigaction sigact;
  700. mosquitto_lib_init();
  701. init_device_db();
  702. pthread_mutex_init(&root_lock, NULL);
  703. config_init();
  704. vcom_init();
  705. for (i = 0; i < MAX_VCOM; i++) {
  706. printf("mqtt modbus vcom_%d uplink thread\r\n", i);
  707. err = pthread_create(&mqtt_modbus_uplink_tid[i], NULL, (void* (*)(void*))mqtt_modbus_uplink_process_thread,
  708. (void*)(&thread_index[i]));
  709. if (err != 0) {
  710. printf("create mqtt modbus vcom_%d uplink thread failed\r\n", i);
  711. exit(EXIT_FAILURE);
  712. }
  713. printf("mqtt modbus vcom_%d dwlink thread\r\n", i);
  714. err = pthread_create(&mqtt_modbus_dwlink_tid[i], NULL, (void* (*)(void*))mqtt_modbus_dwlink_process_thread,
  715. (void*)(&thread_index[i]));
  716. if (err != 0) {
  717. printf("create mqtt modbus vcomd_%d dwlink thread failed\r\n", i);
  718. exit(EXIT_FAILURE);
  719. }
  720. }
  721. // web
  722. printf("http json process thread\r\n");
  723. err = pthread_create(&http_json_tid, NULL, (void* (*)(void*))http_json_process_thread, NULL);
  724. if (err != 0) {
  725. printf("create http json thread failed\r\n");
  726. exit(EXIT_FAILURE);
  727. }
  728. sigemptyset(&sigact.sa_mask);
  729. sigact.sa_flags = 0;
  730. sigact.sa_handler = sig_handler;
  731. sigaction(SIGQUIT, &sigact, NULL);
  732. sigaction(SIGINT, &sigact, NULL);
  733. sigaction(SIGTERM, &sigact, NULL);
  734. while (!exit_sig && !quit_sig) {
  735. pthread_mutex_lock(&root_lock);
  736. get_system_status();
  737. pthread_mutex_unlock(&root_lock);
  738. sleep(120);
  739. }
  740. for (i = 0; i < MAX_VCOM; i++) {
  741. pthread_join(mqtt_modbus_uplink_tid[i], NULL);
  742. pthread_join(mqtt_modbus_dwlink_tid[i], NULL);
  743. }
  744. pthread_join(http_json_tid, NULL);
  745. if (exit_sig || quit_sig) {
  746. // shutdown(net_sock, SHUT_RDWR);
  747. }
  748. pthread_mutex_destroy(&root_lock);
  749. mosquitto_lib_cleanup();
  750. exit(EXIT_SUCCESS);
  751. }