task.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "task.h"
  2. #include "malloc.h"
  3. #include "cjson.h"
  4. #include "myFile.h"
  5. #include "gateway_message.h"
  6. #include "log.h"
  7. #include "sx1276.h"
  8. #include "radio.h"
  9. void processHTTPjson(cJSON *json);
  10. void processDevice(cJSON *json, const char *readString);
  11. void master_task(char *string);
  12. void slave_task();
  13. tRadioDriver *Radio = NULL;
  14. uint16_t BufferSize ;
  15. uint8_t Buffer[256];
  16. uint32_t rx_num = 0;
  17. uint8_t PingMsg[] = "PING\0";
  18. uint8_t PongMsg[] = "PONG\0";
  19. /**
  20. * @brief 从输入字符串匹配对应的key并将value中的string返回
  21. * @param cJSON *data_obj json对象, const char *key key关键字
  22. * @retval 解析出的string
  23. */
  24. char *processStringData(cJSON *data_obj, const char *key)
  25. {
  26. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  27. if (cJSON_IsString(json_data) && (json_data->valuestring != NULL))
  28. {
  29. return json_data->valuestring;
  30. }
  31. else
  32. {
  33. // gateway.data_valid_flag = 0; // 如果任意数据为NULL则此次数据为无效
  34. return NULL;
  35. }
  36. }
  37. // json解析int
  38. int processIntData(cJSON *data_obj, const char *key)
  39. {
  40. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  41. // if (cJSON_IsNumber(json_data) && (json_data->valueint != NULL)) 解决寄存器地址为0问题
  42. if (cJSON_IsNumber(json_data))
  43. {
  44. return json_data->valueint;
  45. }
  46. else
  47. {
  48. // gateway.data_valid_flag = 0; // 如果任意数据为NULL则此次数据为无效
  49. return -1;
  50. }
  51. }
  52. // 解析json中包含的节点数量,已经节点的id
  53. void processNodeListData(cJSON *data_obj, GATEWAY_PARAMS *gateway)
  54. {
  55. cJSON *nodeList = cJSON_GetObjectItemCaseSensitive(data_obj, "nodeList");
  56. if (nodeList == NULL || !cJSON_IsArray(nodeList))
  57. {
  58. cJSON_Delete(nodeList);
  59. }
  60. int nodeListSize = cJSON_GetArraySize(nodeList);
  61. for (int i = 0; i < nodeListSize; i++)
  62. {
  63. cJSON *node = cJSON_GetArrayItem(nodeList, i);
  64. if (cJSON_IsString(node))
  65. {
  66. addNode(gateway, node->valuestring); // 往链表中不断加入新的节点
  67. }
  68. }
  69. }
  70. /*
  71. *********************************************************************************************************
  72. * 函 数 名: void data_task(void *pdata)
  73. * 功能说明: 主要是data_task处理线程,优先级高。其运行逻辑是将nandflash中的数据解析出来轮询发送数据
  74. * 形 参:无
  75. * 返 回 值: 无
  76. *********************************************************************************************************
  77. */
  78. void data_task(void *pdata)
  79. {
  80. OS_CPU_SR cpu_sr;
  81. pdata = pdata;
  82. char *lora_config_json = malloc(15 * 1024);
  83. read_file("lora_json.txt", lora_config_json);
  84. cJSON *json = cJSON_Parse(lora_config_json);
  85. if (json != NULL)
  86. {
  87. OS_ENTER_CRITICAL(); // 进入临界区确保同一时间只有一个线程访问gatewayMessage
  88. processHTTPjson(json);
  89. OS_EXIT_CRITICAL();
  90. }
  91. else
  92. {
  93. LogPrint(LOG_INFO, __func__, __LINE__, "json is to long");
  94. }
  95. cJSON_Delete(json);
  96. free(lora_config_json);
  97. Radio = RadioDriverInit();
  98. Radio->Init();
  99. #ifdef MASTER
  100. Radio->SetTxPacket(PingMsg, strlen((const char *)PingMsg));
  101. printf("MASTER");
  102. #else
  103. Radio->StartRx();
  104. printf("SLAVE");
  105. #endif
  106. while (1)
  107. {
  108. #ifdef MASTER
  109. master_task("a");
  110. #else
  111. slave_task();
  112. #endif
  113. }
  114. }
  115. /*
  116. *********************************************************************************************************
  117. * 函 数 名: void processHTTPjson(cJSON *json)
  118. * 功能说明: 解析http下载json文件数据转换为结构体信息保存在内部结构体
  119. * 形 参:无
  120. * 返 回 值: 无
  121. *********************************************************************************************************
  122. */
  123. void processHTTPjson(cJSON *json)
  124. {
  125. GATEWAY_PARAMS *gateway = get_gateway_config_params();
  126. // 解析必须的网关信息
  127. strcpy(gateway->host, processStringData(json, "host"));
  128. gateway->port = processIntData(json, "port");
  129. strcpy(gateway->messageTopic, processStringData(json, "messageTopic"));
  130. strcpy(gateway->commandTopic, processStringData(json, "commandTopic"));
  131. // 解析节点lodelist内包含的nodelist的设备序列号并扩充整个设备链表
  132. processNodeListData(json, gateway);
  133. NODE_PARAMS *current = gateway->node_params;
  134. // 拿到节点的名称后去解析json的相关内容
  135. while (current != NULL)
  136. {
  137. char *processNodeRead = malloc(20);
  138. sprintf(processNodeRead, "read_%s", current->node_address);
  139. cJSON *readArrayItem = cJSON_GetObjectItem(json, processNodeRead);
  140. if (cJSON_IsArray(readArrayItem))
  141. {
  142. uint8_t read_data_num = cJSON_GetArraySize(readArrayItem);
  143. for (int i = 0; i < read_data_num; i++)
  144. {
  145. cJSON *read_data_obj = cJSON_GetArrayItem(readArrayItem, i);
  146. // 解析到节点内部的内容
  147. if (cJSON_IsObject(read_data_obj))
  148. {
  149. DEVICE_PARAMS *currentDevice;
  150. }
  151. else
  152. {
  153. }
  154. }
  155. }
  156. free(processNodeRead);
  157. current = current->nextNode;
  158. }
  159. }
  160. /*
  161. *********************************************************************************************************
  162. * 函 数 名: void master_task(char *string)
  163. * 功能说明: 主网关sx1278轮询发送调用接口,发送结束后就将状态切换到接收状态,等待从机的响应值,当从机超时或接收到数据进行后续数据处理的流程
  164. * 形 参:无
  165. * 返 回 值: 无
  166. *********************************************************************************************************
  167. */
  168. volatile uint32_t startTime; //记录开始的时间
  169. void master_task(char *string)
  170. {
  171. switch (Radio->Process())
  172. {
  173. case RF_RX_DONE:
  174. Radio->GetRxPacket(Buffer, &BufferSize);
  175. printf("Master_Task:RX_____%s,Rx_num:%d\n", Buffer, rx_num);
  176. rx_num++;
  177. if (strncmp((const char *)Buffer, (const char *)PongMsg, strlen((const char *)PongMsg)) == 0)
  178. {
  179. Radio->SetTxPacket(PingMsg, strlen((const char *)PingMsg));
  180. startTime=OSTimeGet();
  181. delay_ms(1000);
  182. }
  183. break;
  184. case RF_TX_DONE:
  185. startTime=OSTimeGet();
  186. Radio->StartRx();
  187. case RF_BUSY:
  188. case RF_IDLE:
  189. if(OSTimeGet()-startTime>10000) //每次发送信号时记录上一次发送的时间如果超过一段时间没有响应则进行下一次发送
  190. {
  191. Radio->SetTxPacket(PingMsg, strlen((const char *)PingMsg));
  192. startTime=OSTimeGet();
  193. delay_ms(1000);
  194. }
  195. default:
  196. OSTimeDlyHMSM(0, 0, 0, 500);
  197. break;
  198. }
  199. }
  200. /*
  201. *********************************************************************************************************
  202. * 函 数 名: void slave_task(char *string)
  203. * 功能说明: 负责从站数据处理
  204. * 形 参:无
  205. * 返 回 值: 无
  206. *********************************************************************************************************
  207. */
  208. volatile bool rxflag=false;
  209. void slave_task()
  210. {
  211. switch (Radio->Process())
  212. {
  213. case RF_RX_DONE:
  214. Radio->GetRxPacket(Buffer, &BufferSize);
  215. printf("Slave_Task:RX_____%s,Rx_num:%d\n", Buffer, rx_num);
  216. rx_num++;
  217. if (strncmp((const char *)Buffer, (const char *)PingMsg, strlen((const char *)PingMsg)) == 0)
  218. {
  219. Radio->SetTxPacket(PongMsg, strlen((const char *)PongMsg));
  220. delay_ms(1000);
  221. }
  222. break;
  223. case RF_TX_DONE:
  224. Radio->StartRx();
  225. break;
  226. case RF_IDLE:
  227. case RF_BUSY:
  228. default:
  229. OSTimeDlyHMSM(0, 0, 0, 500);
  230. }
  231. }