megatec.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * megatec.c
  3. *
  4. * Created on: 22.05.2017
  5. * Author: balbekova
  6. */
  7. #include "FreeRTOS.h"
  8. #include "task.h"
  9. #include "usart.h"
  10. #include "megatec.h"
  11. #include "log.h"
  12. #include "snmp_api.h"
  13. #ifdef PRINTF_STDLIB
  14. #include <stdio.h>
  15. #endif
  16. #ifdef PRINTF_CUSTOM
  17. #include "tinystdio.h"
  18. #endif
  19. #include <string.h>
  20. #include <math.h>
  21. #define UPS_PDU_MAX_LEN 50
  22. float TimeParamFloat = 0;
  23. uint16_t TimeParam = 0;
  24. uint16_t TimeParam2 = 0;
  25. bool megatec_send = true;
  26. UPS_value_t UPS;
  27. enum {
  28. NORMAL = 0x00,
  29. VER_ERROR = 0x01,
  30. CHKSUM_ERROR = 0x02,
  31. LCHKSUM_ERROR = 0x03,
  32. CID2_INVALID = 0x04,
  33. CMD_FMT_ERROR = 0x05,
  34. INVALID_DATA = 0x06,
  35. };
  36. static struct {
  37. uint8_t data[UPS_PDU_MAX_LEN];
  38. uint16_t len;
  39. } ups_pdu;
  40. const char* MegaTecCMD[] =
  41. {
  42. "Q1\r",
  43. "T\r",
  44. "TL\r",
  45. "T",
  46. "Q\r",
  47. "S",
  48. "R",
  49. "C\r",
  50. "CT\r",
  51. "I\r",
  52. "F\r",
  53. "Q2\r"
  54. };
  55. extern bool flUpdateLog;
  56. void init_UPS_value(void)
  57. {
  58. UPS.Freq_in = 0;
  59. UPS.VAC_in = 0;
  60. UPS.VAC_out = 0;
  61. UPS.Temp = 0;
  62. UPS.Load = 0;
  63. UPS.SOC = 0;
  64. UPS.work_time = 0;
  65. UPS.Status = 0;
  66. UPS.Alarm = 0;
  67. UPS.cnt_err_ups = 0;
  68. UPS.Flag_Present = false;
  69. UPS.Present = false;
  70. memset(UPS.model, 0, 11);
  71. memset(UPS.vertion, 0, 11);
  72. }
  73. void send_MegaTec_cmd(cmdMegaTecEnums_t command)
  74. {
  75. if(command == ups_test_time){
  76. uint8_t req[10];
  77. memset(req, 0, 10);
  78. if(TimeParam < 10)
  79. sprintf(req, "%s0%d\r", MegaTecCMD[command], TimeParam);
  80. else
  81. sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
  82. ups_send_block(req, strlen(req));
  83. }
  84. else if(command == ups_shutdown){
  85. uint8_t req[10];
  86. memset(req, 0, 10);
  87. if(TimeParamFloat >= 1 && TimeParamFloat < 10)
  88. {
  89. sprintf(req, "%s0%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  90. }
  91. else if(TimeParamFloat < 1){
  92. sprintf(req, "%s.%d\r", MegaTecCMD[command], (uint16_t)(10*TimeParamFloat));
  93. }
  94. else{
  95. sprintf(req, "%s%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  96. }
  97. ups_send_block(req, strlen(req));
  98. }
  99. else if(command == ups_shutdown_restore){
  100. uint8_t req[10];
  101. memset(req, 0, 10);
  102. sprintf(req, "%s.%d%s%d\r", MegaTecCMD[command-1], TimeParam, MegaTecCMD[command], TimeParam2);
  103. ups_send_block(req, strlen(req));
  104. }
  105. else{
  106. // TODO ����������� ���������
  107. //ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
  108. ups_send_block((void*)MegaTecCMD[command], strlen(MegaTecCMD[command]));
  109. }
  110. }
  111. bool ups_megatec_rx_pdu(void)
  112. {
  113. int c = 0;
  114. ups_pdu.len = 0;
  115. while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
  116. c = ups_getchar(500);//portMAX_DELAY
  117. if(c < 0)
  118. {
  119. ups_pdu.len = 0;
  120. break;
  121. }
  122. ups_pdu.data[ups_pdu.len++] = c;
  123. }
  124. DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
  125. DBG printf("UPS raw data len: %d\r\n", ups_pdu.len);
  126. if (ups_pdu.len == 0)
  127. return false;
  128. return true;
  129. }
  130. int8_t get_ups_param(char *buf, char *param, char *val)
  131. {
  132. char *endValue;
  133. int8_t param_len;
  134. memset(val, 0, 10);
  135. endValue = strpbrk(buf, param);
  136. if(endValue != NULL){
  137. param_len = endValue - buf;
  138. if(param_len < 10){
  139. strncpy(val, buf, param_len);
  140. }
  141. else{
  142. param_len = 0;
  143. }
  144. }
  145. else{
  146. param_len = 0;
  147. }
  148. DBG printf("UPS parameter: %s\r\n", val);
  149. return param_len;
  150. }
  151. void ups_status_response(char *data)
  152. {
  153. uint8_t i;
  154. char value[10];
  155. uint8_t len = 0;
  156. DBG printf("ups_status_response: %s\r\n", data);
  157. if(data[0] != '(')
  158. return;
  159. DBG printf("ups_status_parser_start\r\n");
  160. UPS.Present = true;
  161. UPS.Flag_Present = true;
  162. UPS.cnt_err_ups = 0;
  163. if(flUpdateLog){
  164. flUpdateLog = false;
  165. log_add(data);
  166. }
  167. data++;
  168. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  169. len = get_ups_param(data, " ", value);
  170. data += (len + 1);
  171. if(len > 0)
  172. UPS.VAC_in = atof(value);
  173. //TODO
  174. len = get_ups_param(data, " ", value);
  175. data += (len + 1);
  176. len = get_ups_param(data, " ", value);
  177. data += (len + 1);
  178. if(len > 0)
  179. UPS.VAC_out = atof(value);
  180. len = get_ups_param(data, " ", value);
  181. data += (len + 1);
  182. if(len > 0)
  183. UPS.Load = atoi(value);
  184. len = get_ups_param(data, " ", value);
  185. data += (len + 1);
  186. if(len > 0)
  187. UPS.Freq_in = atof(value);
  188. //TODO
  189. len = get_ups_param(data, " ", value);
  190. data += (len + 1);
  191. if(len > 0)
  192. UPS.SOC = 100*((atof(value)) - 1.6)/0.7;
  193. len = get_ups_param(data, " ", value);
  194. data += (len + 1);
  195. if(len > 0)
  196. UPS.Temp = atof(value);
  197. len = get_ups_param(data, "\r", value);
  198. data += (len + 1);
  199. if(len > 0){
  200. uint8_t stat = 0;
  201. for(i = 0; i < len; i ++)
  202. {
  203. stat |= (value[i] - 0x30) << (7-i);
  204. }
  205. UPS.Status = stat;
  206. }
  207. }
  208. void ups_info_response(char *data)
  209. {
  210. uint8_t i = 0;
  211. if(data[0] != '#')
  212. return;
  213. UPS.Present = true;
  214. UPS.Flag_Present = true;
  215. UPS.cnt_err_ups = 0;
  216. data++;
  217. data += 16;
  218. while(data[0] == ' '){
  219. data ++;
  220. i ++;
  221. }
  222. if(i < 15){
  223. /*endValue = strpbrk(data," ");
  224. len = endValue - data;*/
  225. if(UPS.model[0] == 0){
  226. strncpy(UPS.model, data, 10);
  227. SNMP_SetObjDescr();
  228. }
  229. else{
  230. strncpy(UPS.model, data, 10);
  231. }
  232. data += 11;
  233. }
  234. else
  235. {
  236. if(UPS.model[0] == 0){
  237. strcpy(UPS.model, "RTMP II");
  238. SNMP_SetObjDescr();
  239. }
  240. else{
  241. strcpy(UPS.model, "RTMP II");
  242. }
  243. data += 11;
  244. }
  245. strncpy(UPS.serial, data, 8);
  246. data += 8;
  247. strncpy(UPS.vertion, data, 2);
  248. }
  249. void ups_remain_time_response(char *data)
  250. {
  251. char value[10];
  252. if(data[0] != '(')
  253. return;
  254. DBG printf("ups_remain_time_response: %s\r\n", data);
  255. UPS.Present = true;
  256. UPS.Flag_Present = true;
  257. UPS.cnt_err_ups = 0;
  258. data++;
  259. if(strlen(data) > 5)
  260. return;
  261. memset(value, 0, 10);
  262. strcpy(value, data);
  263. if((UPS.Status >> 7) & 0x01)
  264. UPS.work_time = atof(value);
  265. else
  266. UPS.work_time = 0;
  267. }
  268. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  269. {
  270. switch(command)
  271. {
  272. case ups_status_req:
  273. ups_status_response(ups_pdu.data);
  274. break;
  275. case ups_info:
  276. ups_info_response(ups_pdu.data);
  277. break;
  278. case ups_rating_info:
  279. break;
  280. case ups_remain_time_reg:
  281. ups_remain_time_response(ups_pdu.data);
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  288. {
  289. if(UPS.Present){
  290. while(!megatec_send)
  291. {
  292. vTaskDelay(50);
  293. }
  294. megatec_send = false;
  295. send_MegaTec_cmd(command);
  296. if (ups_megatec_rx_pdu())
  297. {
  298. megatec_send = true;
  299. if(strncmp(ups_pdu.data, "ACK", 3) == 0)
  300. return 1;
  301. else if(strncmp(ups_pdu.data, "NAK", 3) == 0)
  302. return 0;
  303. }
  304. }
  305. return -1;
  306. }
  307. // TODO ����������� ���������
  308. //void request_task(void)
  309. void request_task(void* params)
  310. {
  311. for(;;)
  312. {
  313. if(UPS.Present == true){
  314. if(UPS.Flag_Present == false)
  315. {
  316. if(UPS.cnt_err_ups != 2)
  317. UPS.cnt_err_ups++;
  318. else{
  319. UPS.Freq_in = 0;
  320. UPS.VAC_in = 0;
  321. UPS.VAC_out = 0;
  322. UPS.Temp = 0;
  323. UPS.Load = 0;
  324. UPS.SOC = 0;
  325. UPS.work_time = 0;
  326. UPS.Status = 0;
  327. UPS.Alarm = 0;
  328. UPS.Present = false;
  329. //memset(UPS.model, 0, 11);
  330. memset(UPS.vertion, 0, 11);
  331. }
  332. }
  333. }
  334. if(megatec_send){
  335. megatec_send= false;
  336. UPS.Flag_Present = false;
  337. send_MegaTec_cmd(ups_status_req);
  338. if (ups_megatec_rx_pdu())
  339. ups_megatec_process_pdu(ups_status_req);
  340. megatec_send=true;
  341. }
  342. if(megatec_send){
  343. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  344. megatec_send= false;
  345. UPS.Flag_Present = false;
  346. send_MegaTec_cmd(ups_remain_time_reg);
  347. if (ups_megatec_rx_pdu())
  348. ups_megatec_process_pdu(ups_remain_time_reg);
  349. megatec_send=true;
  350. }
  351. //vTaskDelay(100);
  352. if(megatec_send){
  353. megatec_send = false;
  354. UPS.Flag_Present = false;
  355. send_MegaTec_cmd(ups_info);
  356. if (ups_megatec_rx_pdu())
  357. ups_megatec_process_pdu(ups_info);
  358. megatec_send = true;
  359. }
  360. vTaskDelay(1000);
  361. }
  362. }
  363. void ups_megatec_init(void) {
  364. init_UPS_value();
  365. UPS.Present = true;
  366. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  367. }