]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/handlers.cpp
Replace == with -eq for numeric comparison
[stg.git] / projects / stargazer / plugins / other / smux / handlers.cpp
1 #include <cassert>
2
3 #include "stg/GetRequest-PDU.h"
4 #include "stg/GetResponse-PDU.h"
5 #include "stg/VarBindList.h"
6 #include "stg/VarBind.h"
7
8 #include "stg/common.h"
9
10 #include "utils.h"
11 #include "smux.h"
12
13 #ifdef SMUX_DEBUG
14 bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus)
15 {
16 printfd(__FILE__, "SMUX::CloseHandler()\n");
17 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
18 return true;
19 }
20 #else
21 bool SMUX::CloseHandler(const SMUX_PDUs_t *)
22 {
23 return true;
24 }
25 #endif
26
27 #ifdef SMUX_DEBUG
28 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
29 {
30 printfd(__FILE__, "SMUX::RegisterResponseHandler()\n");
31 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
32 return true;
33 }
34 #else
35 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t *)
36 {
37 return true;
38 }
39 #endif
40
41 bool SMUX::PDUsRequestHandler(const SMUX_PDUs_t * pdus)
42 {
43 #ifdef SMUX_DEBUG
44 printfd(__FILE__, "SMUX::PDUsRequestHandler()\n");
45 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
46 #endif
47 PDUsHandlers::iterator it;
48 it = pdusHandlers.find(pdus->choice.pdus.present);
49 if (it != pdusHandlers.end())
50     {
51     return (this->*(it->second))(&pdus->choice.pdus);
52     }
53 else
54     {
55 #ifdef SMUX_DEBUG
56     switch (pdus->present)
57         {
58         case PDUs_PR_NOTHING:
59             printfd(__FILE__, "SMUX::PDUsRequestHandler() - nothing\n");
60             break;
61         case PDUs_PR_get_response:
62             printfd(__FILE__, "SMUX::PDUsRequestHandler() - get response\n");
63             break;
64         case PDUs_PR_trap:
65             printfd(__FILE__, "SMUX::PDUsRequestHandler() - trap\n");
66             break;
67         default:
68             printfd(__FILE__, "SMUX::PDUsRequestHandler() - undefined\n");
69         }
70 #endif
71     }
72 return true;
73 }
74
75 #ifdef SMUX_DEBUG
76 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
77 {
78 printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n");
79 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
80 return true;
81 }
82 #else
83 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t *)
84 {
85 return true;
86 }
87 #endif
88
89 bool SMUX::GetRequestHandler(const PDUs_t * pdus)
90 {
91 #ifdef SMUX_DEBUG
92 printfd(__FILE__, "SMUX::GetRequestHandler()\n");
93 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
94 #endif
95 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
96 GetResponse_PDU_t * msg = static_cast<GetResponse_PDU_t *>(calloc(1, sizeof(GetResponse_PDU_t)));
97 assert(msg && "Enought mempry to allocate GetResponse_PDU_t");
98 VarBindList_t * varBindList = &msg->variable_bindings;
99
100 long id = 0;
101 asn_INTEGER2long(&getRequest->request_id, &id);
102 asn_long2INTEGER(&msg->request_id, id);
103 asn_long2INTEGER(&msg->error_status, 0);
104 asn_long2INTEGER(&msg->error_index, 0);
105
106 const VarBindList_t * vbl = &getRequest->variable_bindings; 
107 for (int i = 0; i < vbl->list.count; ++i)
108     {
109     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
110     Sensors::iterator it;
111     it = sensors.find(OID(&vb->name));
112     if (it == sensors.end())
113         {
114         return SendGetResponseErrorPDU(sock, getRequest,
115                                        PDU__error_status_noSuchName, i);
116         }
117
118     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
119     assert(newVb && "Enought mempry to allocate VarBind_t");
120
121     it->first.ToOID(&newVb->name);
122     it->second->GetValue(&newVb->value);
123
124     ASN_SEQUENCE_ADD(varBindList, newVb);
125     }
126
127 bool res = SendGetResponsePDU(sock, msg);
128 #ifdef SMUX_DEBUG
129 asn_fprint(stderr, &asn_DEF_GetResponse_PDU, msg);
130 #endif
131 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
132 return res;
133 }
134
135 bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
136 {
137 #ifdef SMUX_DEBUG
138 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
139 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
140 #endif
141 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
142 GetResponse_PDU_t * msg = static_cast<GetResponse_PDU_t *>(calloc(1, sizeof(GetResponse_PDU_t)));
143 assert(msg && "Enought mempry to allocate GetResponse_PDU_t");
144 VarBindList_t * varBindList = &msg->variable_bindings;
145
146 long id = 0;
147 asn_INTEGER2long(&getRequest->request_id, &id);
148 asn_long2INTEGER(&msg->request_id, id);
149 asn_long2INTEGER(&msg->error_status, 0);
150 asn_long2INTEGER(&msg->error_index, 0);
151
152 const VarBindList_t * vbl = &getRequest->variable_bindings; 
153 for (int i = 0; i < vbl->list.count; ++i)
154     {
155     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
156     Sensors::iterator it;
157     it = sensors.upper_bound(OID(&vb->name));
158     if (it == sensors.end())
159         {
160 #ifdef SMUX_DEBUG
161         printfd(__FILE__, "SMUX::GetNextRequestHandler() - '%s' not found\n", OID(&vb->name).ToString().c_str());
162 #endif
163         return SendGetResponseErrorPDU(sock, getRequest,
164                                        PDU__error_status_noSuchName, i);
165         }
166
167     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
168     assert(newVb && "Enought mempry to allocate VarBind_t");
169
170     it->first.ToOID(&newVb->name);
171     it->second->GetValue(&newVb->value);
172
173     ASN_SEQUENCE_ADD(varBindList, newVb);
174     }
175
176 bool res = SendGetResponsePDU(sock, msg);
177 #ifdef SMUX_DEBUG
178 asn_fprint(stderr, &asn_DEF_PDU, msg);
179 #endif
180 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
181 return res;
182 }
183
184 bool SMUX::SetRequestHandler(const PDUs_t * pdus)
185 {
186 #ifdef SMUX_DEBUG
187 printfd(__FILE__, "SMUX::SetRequestHandler()\n");
188 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
189 #endif
190 return SendGetResponseErrorPDU(sock, &pdus->choice.set_request,
191                                PDU__error_status_readOnly, 0);
192 }