]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/handlers.cpp
\n added
[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 false;
19 }
20 #else
21 bool SMUX::CloseHandler(const SMUX_PDUs_t *)
22 {
23 return false;
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 false;
33 }
34 #else
35 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t *)
36 {
37 return false;
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 false;
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 false;
81 }
82 #else
83 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t *)
84 {
85 return false;
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         SendGetResponseErrorPDU(sock, getRequest,
115                                 PDU__error_status_noSuchName, i);
116         return true;
117         }
118
119     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
120     assert(newVb && "Enought mempry to allocate VarBind_t");
121
122     it->first.ToOID(&newVb->name);
123     it->second->GetValue(&newVb->value);
124
125     ASN_SEQUENCE_ADD(varBindList, newVb);
126     }
127
128 SendGetResponsePDU(sock, msg);
129 #ifdef SMUX_DEBUG
130 asn_fprint(stderr, &asn_DEF_GetResponse_PDU, msg);
131 #endif
132 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
133 return false;
134 }
135
136 bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
137 {
138 #ifdef SMUX_DEBUG
139 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
140 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
141 #endif
142 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
143 GetResponse_PDU_t * msg = static_cast<GetResponse_PDU_t *>(calloc(1, sizeof(GetResponse_PDU_t)));
144 assert(msg && "Enought mempry to allocate GetResponse_PDU_t");
145 VarBindList_t * varBindList = &msg->variable_bindings;
146
147 long id = 0;
148 asn_INTEGER2long(&getRequest->request_id, &id);
149 asn_long2INTEGER(&msg->request_id, id);
150 asn_long2INTEGER(&msg->error_status, 0);
151 asn_long2INTEGER(&msg->error_index, 0);
152
153 const VarBindList_t * vbl = &getRequest->variable_bindings; 
154 for (int i = 0; i < vbl->list.count; ++i)
155     {
156     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
157     Sensors::iterator it;
158     it = sensors.upper_bound(OID(&vb->name));
159     if (it == sensors.end())
160         {
161 #ifdef SMUX_DEBUG
162         printfd(__FILE__, "SMUX::GetNextRequestHandler() - '%s' not found\n", OID(&vb->name).ToString().c_str());
163 #endif
164         SendGetResponseErrorPDU(sock, getRequest,
165                                 PDU__error_status_noSuchName, i);
166         return true;
167         }
168
169     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
170     assert(newVb && "Enought mempry to allocate VarBind_t");
171
172     it->first.ToOID(&newVb->name);
173     it->second->GetValue(&newVb->value);
174
175     ASN_SEQUENCE_ADD(varBindList, newVb);
176     }
177
178 SendGetResponsePDU(sock, msg);
179 #ifdef SMUX_DEBUG
180 asn_fprint(stderr, &asn_DEF_PDU, msg);
181 #endif
182 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
183 return false;
184 }
185
186 bool SMUX::SetRequestHandler(const PDUs_t * pdus)
187 {
188 #ifdef SMUX_DEBUG
189 printfd(__FILE__, "SMUX::SetRequestHandler()\n");
190 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
191 #endif
192 SendGetResponseErrorPDU(sock, &pdus->choice.set_request,
193                         PDU__error_status_readOnly, 0);
194 return false;
195 }