snmp_api.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /********************************* (C) ROTEK ***********************************
  2. * @module template
  3. * @file template.c
  4. * @version 1.0.0
  5. * @date XX.XX.XXXX
  6. * $brief Template
  7. *******************************************************************************
  8. * @history Version Author Comment
  9. * XX.XX.XXXX 1.0.0 Telenkov D.A. First release.
  10. *******************************************************************************
  11. */
  12. #include "stm32f4xx.h"
  13. #include "snmp_api.h"
  14. #include "trap_api.h"
  15. #include "settings_api.h"
  16. #include "common_config.h"
  17. #include "rtc.h"
  18. #include "megatec.h"
  19. #ifdef PRINTF_STDLIB
  20. #include <stdio.h>
  21. #endif
  22. #ifdef PRINTF_CUSTOM
  23. #include "tinystdio.h"
  24. #endif
  25. #include <stdbool.h>
  26. #include <string.h>
  27. #include "lwip/opt.h"
  28. #include "lwip/api.h"
  29. #include "lwip/sys.h"
  30. #include "lwip/udp.h"
  31. #include "snmp.h"
  32. #include "snmp_msg.h"
  33. #include "private_mib.h"
  34. #include "FreeRTOS.h"
  35. #include "task.h"
  36. #include "queue.h"
  37. /**
  38. * @brief Пул всех возможных трапов устройства
  39. */
  40. extern TRAP_t traps[];
  41. /**
  42. * @brief Общая структура настроек
  43. */
  44. extern SETTINGS_t sSettings;
  45. /**
  46. * @brief Очередь для отправки трапов
  47. */
  48. QueueHandle_t SNMP_TrapQueue;
  49. /**
  50. * @brief Инициализация параметров SNMP
  51. * @retval
  52. */
  53. void SNMP_Init(void)
  54. {
  55. //SETTINGS_SetSnmpDef();
  56. SNMP_AgentInit();
  57. }
  58. /**
  59. * @brief Системный тик SNMP. Таск должен вызываться с частотой 100Гц.
  60. * @retval
  61. */
  62. void SNMP_SysUpTimeTask(void *arg)
  63. {
  64. TickType_t xLastWakeTime;
  65. const TickType_t xFrequency = 10;
  66. xLastWakeTime = xTaskGetTickCount();
  67. while(1)
  68. {
  69. vTaskDelayUntil( &xLastWakeTime, xFrequency );
  70. snmp_inc_sysuptime();
  71. }
  72. }
  73. /**
  74. * @brief Тестовый таск для проверки отправки трапов
  75. * @retval
  76. */
  77. void snmp_trap_tread(void *arg)
  78. {
  79. uint8_t trapName;
  80. while(1)
  81. {
  82. if (xQueueReceive(SNMP_TrapQueue, &trapName, 0) == pdTRUE)
  83. {
  84. SNMP_SetManagerIP(sSettings.sSnmp.managerIP);
  85. SNMP_SendVarbindTrap(&traps[trapName]);
  86. SNMP_SetManagerIP(sSettings.sSnmp.managerIP2);
  87. SNMP_SendVarbindTrap(&traps[trapName]);
  88. SNMP_SetManagerIP(sSettings.sSnmp.managerIP3);
  89. SNMP_SendVarbindTrap(&traps[trapName]);
  90. SNMP_SetManagerIP(sSettings.sSnmp.managerIP4);
  91. SNMP_SendVarbindTrap(&traps[trapName]);
  92. SNMP_SetManagerIP(sSettings.sSnmp.managerIP5);
  93. SNMP_SendVarbindTrap(&traps[trapName]);
  94. }
  95. }
  96. }
  97. /**
  98. * @brief Инициализация SNMP агента
  99. * @retval
  100. */
  101. void SNMP_AgentInit(void)
  102. {
  103. SNMP_SetObjDescr();
  104. SNMP_SetReadCommunity(sSettings.sSnmp.readCommunity);
  105. SNMP_SetWriteCommunity(sSettings.sSnmp.writeCommunity);
  106. SNMP_SetSysContact(sSettings.sSnmp.sysContact);
  107. SNMP_SetSysName(sSettings.sSnmp.sysName);
  108. SNMP_SetSysLocation(sSettings.sSnmp.sysLocation);
  109. SNMP_SetManagerIP(sSettings.sSnmp.managerIP);
  110. SNMP_SetObjID();
  111. SNMP_SetTrapOnOff(1);
  112. SNMP_InitTrapsBase();
  113. snmp_init();
  114. udp_init();
  115. SNMP_TrapQueue = xQueueCreate(SNMP_TRAP_QUEUE_SIZE, sizeof(uint8_t));
  116. }
  117. /**
  118. * @brief Пользовательская функция для отправки трапа из массива traps[]
  119. * Трап помещается в очередь. Работа с очередью происходит по принципу
  120. * FIFO буфера.
  121. * Если в настройках трапа отправка отключена, то трап игнорируется.
  122. * @retval
  123. */
  124. void SNMP_SendUserTrap(uint8_t trapName)
  125. {
  126. uint16_t availableSpace;
  127. uint8_t dummyTrap;
  128. if (traps[trapName].trapEnable)
  129. {
  130. availableSpace = uxQueueSpacesAvailable(SNMP_TrapQueue);
  131. if (availableSpace == 0)
  132. xQueueReceive(SNMP_TrapQueue, &dummyTrap, 0);
  133. xQueueSend(SNMP_TrapQueue, &trapName, 0);
  134. }
  135. }
  136. /**
  137. * @brief Установить SNMP Descriptor
  138. * @retval
  139. */
  140. // TODO
  141. void SNMP_SetObjDescr(void)
  142. {
  143. static uint8_t len;
  144. strcpy(sSettings.sSnmp.sysDescr, sSettings.sSnmp.sysName);
  145. strcat(sSettings.sSnmp.sysDescr, " ");
  146. strcat(sSettings.sSnmp.sysDescr, VERSION);
  147. strcat(sSettings.sSnmp.sysDescr, " ");
  148. strcat(sSettings.sSnmp.sysDescr, sSettings.sSnmp.sysContact);
  149. strcat(sSettings.sSnmp.sysDescr, " ");
  150. strcat(sSettings.sSnmp.sysDescr, sSettings.sInfo.serialNumber);
  151. strcat(sSettings.sSnmp.sysDescr, " ");
  152. strcat(sSettings.sSnmp.sysDescr, UPS.model);
  153. len = strlen(sSettings.sSnmp.sysDescr);
  154. snmp_set_sysdesr((u8_t*)sSettings.sSnmp.sysDescr, &len);
  155. }
  156. /**
  157. * @brief Установить SNMP Community для чтения
  158. */
  159. void SNMP_SetReadCommunity(char *comm)
  160. {
  161. strcpy(sSettings.sSnmp.readCommunity, comm);
  162. }
  163. /**
  164. * @brief Установить SNMP Community для записи
  165. */
  166. void SNMP_SetWriteCommunity(char *comm)
  167. {
  168. strcpy(sSettings.sSnmp.writeCommunity, comm);
  169. }
  170. /**
  171. * @brief Установить SNMP SysContact
  172. * @retval
  173. */
  174. void SNMP_SetSysContact(char *con)
  175. {
  176. static uint8_t len;
  177. len = strlen(con);
  178. snmp_set_syscontact((u8_t*)con, &len);
  179. }
  180. /**
  181. * @brief Установить SNMP SysName
  182. * @retval
  183. */
  184. void SNMP_SetSysName(char *name)
  185. {
  186. static uint8_t len;
  187. len = strlen(name);
  188. snmp_set_sysname((u8_t*)name, &len);
  189. }
  190. /**
  191. * @brief Установить SNMP SysLocation
  192. * @retval
  193. */
  194. void SNMP_SetSysLocation(char *loc)
  195. {
  196. static uint8_t len;
  197. len = strlen(loc);
  198. snmp_set_syslocation((u8_t*)loc, &len);
  199. }
  200. /**
  201. * @brief Установить SNMP SysManagerIP
  202. * @retval
  203. */
  204. void SNMP_SetManagerIP(char *ip)
  205. {
  206. static ip_addr_t trap_addr;
  207. ipaddr_aton(ip, &trap_addr);
  208. snmp_trap_dst_ip_set(0, &trap_addr);
  209. }
  210. /**
  211. * @brief Установить SNMP Object ID
  212. * @retval
  213. */
  214. void SNMP_SetObjID(void)
  215. {
  216. static struct snmp_obj_id my_object_id = {9, {1, 3, 6, 1, 4, 1, 41752, 911, 3}};
  217. snmp_set_sysobjid(&my_object_id);
  218. }
  219. /**
  220. * @brief Вкл/выкл трапы
  221. * @retval
  222. */
  223. void SNMP_SetTrapOnOff(uint8_t state)
  224. {
  225. snmp_trap_dst_enable(0, (u8_t)state);
  226. }
  227. /********************************* (C) ROTEK **********************************/