megatec.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
  79. ups_send_block(req, strlen(req));
  80. }
  81. else if(command == ups_shutdown){
  82. uint8_t req[10];
  83. memset(req, 0, 10);
  84. if(TimeParamFloat >= 1 && TimeParamFloat < 10)
  85. {
  86. sprintf(req, "%s0%f\r", MegaTecCMD[command], TimeParamFloat);
  87. }
  88. else if(TimeParamFloat < 1){
  89. sprintf(req, "%s.%f\r", MegaTecCMD[command], 10*TimeParamFloat);
  90. }
  91. else{
  92. sprintf(req, "%s%f\r", MegaTecCMD[command], TimeParamFloat);
  93. }
  94. ups_send_block(req, strlen(req));
  95. }
  96. else if(command == ups_shutdown_restore){
  97. uint8_t req[10];
  98. memset(req, 0, 10);
  99. sprintf(req, "%s.%d%s%d\r", MegaTecCMD[command-1], TimeParam, MegaTecCMD[command], TimeParam2);
  100. ups_send_block(req, strlen(req));
  101. }
  102. else{
  103. // TODO ����������� ���������
  104. //ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
  105. ups_send_block((void*)MegaTecCMD[command], strlen(MegaTecCMD[command]));
  106. }
  107. }
  108. bool ups_megatec_rx_pdu(void)
  109. {
  110. int c = 0;
  111. ups_pdu.len = 0;
  112. while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
  113. c = ups_getchar(500);//portMAX_DELAY
  114. if(c < 0)
  115. {
  116. ups_pdu.len = 0;
  117. break;
  118. }
  119. ups_pdu.data[ups_pdu.len++] = c;
  120. }
  121. DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
  122. DBG printf("UPS raw data len: %d\r\n", ups_pdu.len);
  123. if (ups_pdu.len == 0)
  124. return false;
  125. return true;
  126. }
  127. int8_t get_ups_param(char *buf, char *param, char *val)
  128. {
  129. char *endValue;
  130. int8_t param_len;
  131. memset(val, 0, 10);
  132. endValue = strpbrk(buf, param);
  133. if(endValue != NULL){
  134. param_len = endValue - buf;
  135. if(param_len < 10){
  136. strncpy(val, buf, param_len);
  137. }
  138. else{
  139. param_len = 0;
  140. }
  141. }
  142. else{
  143. param_len = 0;
  144. }
  145. DBG printf("UPS parameter: %s\r\n", val);
  146. return param_len;
  147. }
  148. void ups_status_response(char *data)
  149. {
  150. uint8_t i;
  151. char value[10];
  152. uint8_t len = 0;
  153. DBG printf("ups_status_response: %s\r\n", data);
  154. if(data[0] != '(')
  155. return;
  156. DBG printf("ups_status_parser_start\r\n");
  157. UPS.Present = true;
  158. UPS.Flag_Present = true;
  159. UPS.cnt_err_ups = 0;
  160. if(flUpdateLog){
  161. flUpdateLog = false;
  162. log_add(data);
  163. }
  164. data++;
  165. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  166. len = get_ups_param(data, " ", value);
  167. data += (len + 1);
  168. if(len > 0)
  169. UPS.VAC_in = atof(value);
  170. //TODO
  171. len = get_ups_param(data, " ", value);
  172. data += (len + 1);
  173. len = get_ups_param(data, " ", value);
  174. data += (len + 1);
  175. if(len > 0)
  176. UPS.VAC_out = atof(value);
  177. len = get_ups_param(data, " ", value);
  178. data += (len + 1);
  179. if(len > 0)
  180. UPS.Load = atoi(value);
  181. len = get_ups_param(data, " ", value);
  182. data += (len + 1);
  183. if(len > 0)
  184. UPS.Freq_in = atof(value);
  185. //TODO
  186. len = get_ups_param(data, " ", value);
  187. data += (len + 1);
  188. if(len > 0)
  189. UPS.SOC = 100*((atof(value)) - 1.6)/0.7;
  190. len = get_ups_param(data, " ", value);
  191. data += (len + 1);
  192. if(len > 0)
  193. UPS.Temp = atof(value);
  194. len = get_ups_param(data, "\r", value);
  195. data += (len + 1);
  196. if(len > 0){
  197. uint8_t stat = 0;
  198. for(i = 0; i < len; i ++)
  199. {
  200. stat |= (value[i] - 0x30) << (7-i);
  201. }
  202. UPS.Status = stat;
  203. }
  204. }
  205. void ups_info_response(char *data)
  206. {
  207. uint8_t i = 0;
  208. if(data[0] != '#')
  209. return;
  210. UPS.Present = true;
  211. UPS.Flag_Present = true;
  212. UPS.cnt_err_ups = 0;
  213. data++;
  214. data += 16;
  215. while(data[0] == ' '){
  216. data ++;
  217. i ++;
  218. }
  219. if(i < 15){
  220. /*endValue = strpbrk(data," ");
  221. len = endValue - data;*/
  222. if(UPS.model[0] == 0){
  223. strncpy(UPS.model, data, 10);
  224. SNMP_SetObjDescr();
  225. }
  226. else{
  227. strncpy(UPS.model, data, 10);
  228. }
  229. data += 11;
  230. }
  231. else
  232. {
  233. if(UPS.model[0] == 0){
  234. strcpy(UPS.model, "RTMP II");
  235. SNMP_SetObjDescr();
  236. }
  237. else{
  238. strcpy(UPS.model, "RTMP II");
  239. }
  240. data += 11;
  241. }
  242. strncpy(UPS.serial, data, 8);
  243. data += 8;
  244. strncpy(UPS.vertion, data, 2);
  245. }
  246. void ups_remain_time_response(char *data)
  247. {
  248. char value[10];
  249. if(data[0] != '(')
  250. return;
  251. DBG printf("ups_remain_time_response: %s\r\n", data);
  252. UPS.Present = true;
  253. UPS.Flag_Present = true;
  254. UPS.cnt_err_ups = 0;
  255. data++;
  256. if(strlen(data) > 5)
  257. return;
  258. memset(value, 0, 10);
  259. strcpy(value, data);
  260. if((UPS.Status >> 7) & 0x01)
  261. UPS.work_time = atof(value);
  262. else
  263. UPS.work_time = 0;
  264. }
  265. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  266. {
  267. switch(command)
  268. {
  269. case ups_status_req:
  270. ups_status_response(ups_pdu.data);
  271. break;
  272. case ups_info:
  273. ups_info_response(ups_pdu.data);
  274. break;
  275. case ups_rating_info:
  276. break;
  277. case ups_remain_time_reg:
  278. ups_remain_time_response(ups_pdu.data);
  279. break;
  280. default:
  281. break;
  282. }
  283. }
  284. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  285. {
  286. if(UPS.Present){
  287. while(!megatec_send)
  288. {
  289. vTaskDelay(50);
  290. }
  291. megatec_send = false;
  292. send_MegaTec_cmd(command);
  293. if (ups_megatec_rx_pdu())
  294. {
  295. megatec_send = true;
  296. if(strncmp(ups_pdu.data, "ACK", 3) == 0)
  297. return 1;
  298. else if(strncmp(ups_pdu.data, "NCK", 3) == 0)
  299. return 0;
  300. }
  301. }
  302. return -1;
  303. }
  304. // TODO ����������� ���������
  305. //void request_task(void)
  306. void request_task(void* params)
  307. {
  308. for(;;)
  309. {
  310. if(UPS.Present == true){
  311. if(UPS.Flag_Present == false)
  312. {
  313. if(UPS.cnt_err_ups != 2)
  314. UPS.cnt_err_ups++;
  315. else{
  316. UPS.Freq_in = 0;
  317. UPS.VAC_in = 0;
  318. UPS.VAC_out = 0;
  319. UPS.Temp = 0;
  320. UPS.Load = 0;
  321. UPS.SOC = 0;
  322. UPS.work_time = 0;
  323. UPS.Status = 0;
  324. UPS.Alarm = 0;
  325. UPS.Present = false;
  326. //memset(UPS.model, 0, 11);
  327. memset(UPS.vertion, 0, 11);
  328. }
  329. }
  330. }
  331. if(megatec_send){
  332. megatec_send= false;
  333. UPS.Flag_Present = false;
  334. send_MegaTec_cmd(ups_status_req);
  335. if (ups_megatec_rx_pdu())
  336. ups_megatec_process_pdu(ups_status_req);
  337. megatec_send=true;
  338. }
  339. if(megatec_send){
  340. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  341. megatec_send= false;
  342. UPS.Flag_Present = false;
  343. send_MegaTec_cmd(ups_remain_time_reg);
  344. if (ups_megatec_rx_pdu())
  345. ups_megatec_process_pdu(ups_remain_time_reg);
  346. megatec_send=true;
  347. }
  348. //vTaskDelay(100);
  349. if(megatec_send){
  350. megatec_send = false;
  351. UPS.Flag_Present = false;
  352. send_MegaTec_cmd(ups_info);
  353. if (ups_megatec_rx_pdu())
  354. ups_megatec_process_pdu(ups_info);
  355. megatec_send = true;
  356. }
  357. vTaskDelay(1000);
  358. }
  359. }
  360. void ups_megatec_init(void) {
  361. init_UPS_value();
  362. UPS.Present = true;
  363. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  364. }