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