snmp_api.c 8.2 KB

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