snmp_api.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #include "log.h"
  20. #include "apps/snmp.h"
  21. #include "snmp_mib2.h"
  22. #include "snmp_core.h"
  23. #include "private_mib.h"
  24. #include "snmp_trap_pdu2.h"
  25. #ifdef PRINTF_STDLIB
  26. #include <stdio.h>
  27. #endif
  28. #ifdef PRINTF_CUSTOM
  29. #include "tinystdio.h"
  30. #endif
  31. #include <stdbool.h>
  32. #include <string.h>
  33. #include "lwip/opt.h"
  34. #include "lwip/api.h"
  35. #include "lwip/sys.h"
  36. #include "lwip/udp.h"
  37. //#include "snmp.h"
  38. //#include "snmp_msg.h"
  39. //#include "private_mib.h"
  40. #include "FreeRTOS.h"
  41. #include "task.h"
  42. #include "queue.h"
  43. /**
  44. * @brief Пул всех возможных трапов устройства
  45. */
  46. extern TRAP_t traps[];
  47. /**
  48. * @brief Общая структура настроек
  49. */
  50. extern SETTINGS_t sSettings;
  51. extern bool isIpReceived;
  52. /**
  53. * @brief Очередь для отправки трапов
  54. */
  55. QueueHandle_t SNMP_TrapQueue;
  56. /**
  57. * @brief Инициализация параметров SNMP
  58. * @retval
  59. */
  60. void SNMP_Init(void)
  61. {
  62. //snmp_mib *my_snmp_mibs[] = { &mib2, &private_mib };
  63. //SETTINGS_SetSnmpDef();
  64. SNMP_AgentInit();
  65. lwip_privmib_init();
  66. //snmp_set_mibs(my_snmp_mibs, LWIP_ARRAYSIZE(my_snmp_mibs));
  67. }
  68. /**
  69. * @brief Системный тик SNMP. Таск должен вызываться с частотой 100Гц.
  70. * @retval
  71. */
  72. void SNMP_SysUpTimeTask(void *arg)
  73. {
  74. TickType_t xLastWakeTime;
  75. const TickType_t xFrequency = 10;
  76. xLastWakeTime = xTaskGetTickCount();
  77. while (1) {
  78. vTaskDelayUntil( &xLastWakeTime, xFrequency );
  79. //snmp_inc_sysuptime();
  80. }
  81. }
  82. /**
  83. * @brief Тестовый таск для проверки отправки трапов
  84. * @retval
  85. */
  86. void snmp_trap_tread(void *arg)
  87. {
  88. uint8_t trapName;
  89. while (1) {
  90. if (isIpReceived) {
  91. if (xQueueReceive(SNMP_TrapQueue, &trapName, 0) == pdTRUE) {
  92. #ifdef NOTIFICATION_CONTROL_ENABLE
  93. if (sSettings.sFlagNotification[trapName])
  94. #endif
  95. {
  96. SNMP_SetManagerIP(sSettings.sSnmp.managerIP);
  97. SNMP_SendVarbindTrap(&traps[trapName]);
  98. SNMP_SetManagerIP(sSettings.sSnmp.managerIP2);
  99. SNMP_SendVarbindTrap(&traps[trapName]);
  100. SNMP_SetManagerIP(sSettings.sSnmp.managerIP3);
  101. SNMP_SendVarbindTrap(&traps[trapName]);
  102. SNMP_SetManagerIP(sSettings.sSnmp.managerIP4);
  103. SNMP_SendVarbindTrap(&traps[trapName]);
  104. SNMP_SetManagerIP(sSettings.sSnmp.managerIP5);
  105. SNMP_SendVarbindTrap(&traps[trapName]);
  106. }
  107. }
  108. }
  109. vTaskDelay(10);
  110. }
  111. }
  112. /**
  113. * @brief Инициализация SNMP агента
  114. * @retval
  115. */
  116. void SNMP_AgentInit(void)
  117. {
  118. SNMP_SetObjDescr();
  119. SNMP_SetReadCommunity(sSettings.sSnmp.readCommunity);
  120. SNMP_SetWriteCommunity(sSettings.sSnmp.writeCommunity);
  121. SNMP_SetSysContact(&sSettings.sSnmp.sysContact);
  122. SNMP_SetSysName(&sSettings.sSnmp.sysName);
  123. SNMP_SetSysLocation(&sSettings.sSnmp.sysLocation);
  124. SNMP_SetManagerIP(sSettings.sSnmp.managerIP); //SNMP_SetManagerIP("192.168.14.37");
  125. SNMP_SetObjID();
  126. SNMP_SetTrapOnOff(1);
  127. SNMP_InitTrapsBase();
  128. snmp_init();
  129. udp_init();
  130. SNMP_TrapQueue = xQueueCreate(SNMP_TRAP_QUEUE_SIZE, sizeof(uint8_t));
  131. }
  132. /**
  133. * @brief Пользовательская функция для отправки трапа из массива traps[]
  134. * Трап помещается в очередь. Работа с очередью происходит по принципу
  135. * FIFO буфера.
  136. * Если в настройках трапа отправка отключена, то трап игнорируется.
  137. * @retval
  138. */
  139. void SNMP_SendUserTrap(uint8_t trapName)
  140. {
  141. uint16_t availableSpace;
  142. uint8_t dummyTrap;
  143. if (traps[trapName].trapEnable) {
  144. availableSpace = uxQueueSpacesAvailable(SNMP_TrapQueue);
  145. if (availableSpace == 0) {
  146. xQueueReceive(SNMP_TrapQueue, &dummyTrap, 0);
  147. }
  148. xQueueSend(SNMP_TrapQueue, &trapName, 0);
  149. }
  150. }
  151. /**
  152. * @brief Установить SNMP Descriptor
  153. * @retval
  154. */
  155. // TODO
  156. void SNMP_SetObjDescr(void)
  157. {
  158. memset(sSettings.sSnmp.sysDesc.description, 0, sizeof(sSettings.sSnmp.sysDesc.description));
  159. strcpy(sSettings.sSnmp.sysDesc.description, HW_REV);
  160. strcat(sSettings.sSnmp.sysDesc.description, " ");
  161. strcat(sSettings.sSnmp.sysDesc.description, VERSION);
  162. strcat(sSettings.sSnmp.sysDesc.description, " ");
  163. sSettings.sSnmp.sysContact.contact[sSettings.sSnmp.sysContact.len] = 0;
  164. strcat(sSettings.sSnmp.sysDesc.description, sSettings.sSnmp.sysContact.contact);
  165. strcat(sSettings.sSnmp.sysDesc.description, " ");
  166. strcat(sSettings.sSnmp.sysDesc.description, sSettings.sInfo.serialNumber);
  167. strcat(sSettings.sSnmp.sysDesc.description, " ");
  168. strcat(sSettings.sSnmp.sysDesc.description, UPS.model);
  169. strcat(sSettings.sSnmp.sysDesc.description, " ");
  170. strcat(sSettings.sSnmp.sysDesc.description, UPS.serial);
  171. sSettings.sSnmp.sysDesc.len = strlen(sSettings.sSnmp.sysDesc.description);
  172. snmp_mib2_set_sysdescr((u8_t *)sSettings.sSnmp.sysDesc.description, &sSettings.sSnmp.sysDesc.len);
  173. }
  174. void SNMP_SettingsSave(void)
  175. {
  176. SETTINGS_Save();
  177. log_event_data(LOG_SETTING_SAVE, "Администратор (SNMP)");
  178. SNMP_SetObjDescr();
  179. }
  180. /**
  181. * @brief Установить SNMP Community для чтения
  182. */
  183. void SNMP_SetReadCommunity(char *comm)
  184. {
  185. //strcpy(sSettings.sSnmp.readCommunity, comm);
  186. snmp_set_community(comm);
  187. }
  188. /**
  189. * @brief Установить SNMP Community для записи
  190. */
  191. void SNMP_SetWriteCommunity(char *comm)
  192. {
  193. //strcpy(sSettings.sSnmp.writeCommunity, comm);
  194. snmp_set_community_write(comm);
  195. }
  196. /**
  197. * @brief Установить SNMP SysContact
  198. * @retval
  199. */
  200. void SNMP_SetSysContact(SNMP_SYS_CONTACT_t *con)
  201. {
  202. snmp_mib2_set_syscontact((u8_t *)con->contact, &con->len, (sizeof(con->contact) - 1));
  203. }
  204. /**
  205. * @brief Установить SNMP SysName
  206. * @retval
  207. */
  208. void SNMP_SetSysName(SNMP_SYS_NAME_t *name)
  209. {
  210. snmp_mib2_set_sysname((u8_t *)name->name, &name->len, (sizeof(name->name) - 1));
  211. }
  212. /**
  213. * @brief Установить SNMP SysLocation
  214. * @retval
  215. */
  216. void SNMP_SetSysLocation(SNMP_SYS_LOCATION_t *loc)
  217. {
  218. snmp_mib2_set_syslocation((u8_t *)loc->location, &loc->len, (sizeof(loc->location) - 1));
  219. }
  220. /**
  221. * @brief Установить SNMP SysManagerIP
  222. * @retval
  223. */
  224. void SNMP_SetManagerIP(char *ip)
  225. {
  226. static ip_addr_t trap_addr;
  227. ipaddr_aton(ip, &trap_addr);
  228. // snmp_trap_dst_ip_set(0, &trap_addr);
  229. snmp_trap_pduv2_dst_ip_set(0, &trap_addr);
  230. }
  231. /**
  232. * @brief Установить SNMP Object ID
  233. * @retval
  234. */
  235. void SNMP_SetObjID(void)
  236. {
  237. static struct snmp_obj_id my_object_id = {9, {1, 3, 6, 1, 4, 1, 41752, 911, SNMP_DEV_ROOT_OID}};
  238. snmp_set_device_enterprise_oid(&my_object_id);
  239. }
  240. /**
  241. * @brief Вкл/выкл трапы
  242. * @retval
  243. */
  244. void SNMP_SetTrapOnOff(uint8_t state)
  245. {
  246. //snmp_trap_dst_enable(0, (u8_t)state);
  247. snmp_trap_pduv2_dst_enable(0, (u8_t)state);
  248. }
  249. /********************************* (C) ROTEK **********************************/